Start learning free

LESSON 114 OF 677 · ARRAYS I

Checking an ArrayBuffer for a Value in Scala

Lesson Read the full explanation

TL ; DR:

buf.contains(x) is true when the ArrayBuffer holds x, and false when it doesn't. Use it as the condition of an if.


val colors = scala.collection.mutable.ArrayBuffer("red", "green", "blue")

println(colors.contains("red"))    // true
println(colors.contains("pink"))   // false

if (!colors.contains("pink")) {
    println("No pink yet")
}


Full lesson:

Often you need to know if an ArrayBuffer holds some value. Is this name on the guest list? Is this item in the cart?

contains(x) answers that. It compares each item with x, from the first one on. It returns true as soon as one matches, or false if none does.

Put ! in front for the opposite. So !colors.contains(x) is true when x is missing.

The answer is a Boolean, so it fits right into an if:

val cart = scala.collection.mutable.ArrayBuffer("milk", "bread")

if (cart.contains("milk")) {
    println("Milk is in the cart")
}

Scala has no in keyword for this. Ask the collection itself with contains.

Text must match exactly. So colors.contains("Red") is false, because the ArrayBuffer has "red".


Your task

bag holds what is packed for a hike.

If "water" is in bag, print Water is packed. If "tent" is not in bag, print Pack a tent.

Expected output:

Water is packed
Pack a tent
STEP 1 OF 2
    Ctrl + Enter to check

    SCALA · ARRAYS I

    Array Membership in Scala, explained

    Is this value in the buffer?

    Programs often need to know whether a collection holds a value: is this name on the guest list, is this item in the cart? On an ArrayBuffer the answer comes from contains(x). It compares the items with x from the first one on, and returns true as soon as one matches or false if none does.

    val colors = scala.collection.mutable.ArrayBuffer("red", "green", "blue")
    println(colors.contains("red"))    // true
    println(colors.contains("pink"))   // false
    

    Scala has no in keyword for this. You ask the collection itself.

    Using it in an if

    The result is a Boolean, so it works directly as the condition of an if:

    val cart = scala.collection.mutable.ArrayBuffer("milk", "bread")
    if (cart.contains("milk")) {
        println("Milk is in the cart")
    }
    

    Put ! in front to turn the answer around. !cart.contains("eggs") is true when the cart has no eggs:

    if (!cart.contains("eggs")) {
        println("No eggs yet")
    }
    

    Things to watch

    • Text must match exactly. colors.contains("Red") is false, because the buffer holds "red". Upper and lower case are different characters.
    • It checks whole items. colors.contains("re") is false too: no item equals "re", even though "red" starts with it.
    • The type of the value matters. In a buffer of numbers such as ArrayBuffer(1, 2, 3), contains(2) is true. Asking for the text "2" still compiles, but it is false, because a number never equals a string.
    • Only equality. contains asks "is there an item equal to x?". For a different question, such as "is there an item above 2?", use exists: nums.exists(_ > 2).

    Why check first

    Asking before acting keeps a program from doing the same thing twice. A guest list that already has a name shouldn't get it again, and a message such as "Pack a tent" should only appear when there is no tent. In both cases the code reads the same way: a question that gives true or false, then an if that acts on the answer. Because contains starts at the first item and stops at the first match, it is quick on short buffers like these. On a very long buffer with no match it has to look at every item.

    Two checks, two answers

    Each if is independent. In the task below, both lines can print, only one can, or neither, depending on what is in the bag. Nothing links them, and an else is not needed because the two questions are about different values. Use else only when one question has two outcomes.

    The task

    bag holds what is packed for a hike. Print Water is packed if "water" is in the bag, and Pack a tent if "tent" is not.

    1. Write an if whose condition is bag.contains("water"). The bag has water, so the condition is true and the first line prints.
    2. Write a second if with !bag.contains("tent"). The bag has no tent, so contains gives false, ! makes it true, and the second line prints.
    val bag = scala.collection.mutable.ArrayBuffer("map", "water", "snacks")
    if (bag.contains("water")) {
        println("Water is packed")
    }
    if (!bag.contains("tent")) {
        println("Pack a tent")
    }
    

    It prints:

    Water is packed
    Pack a tent
    

    Key takeaways

    • buf.contains(x) is true when some item equals x, otherwise false.
    • Use it as an if condition; add ! to test for a missing value.
    • The match is exact, including case, and it is about whole items.
    Keep going with Scala500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free