What a set is
A buffer keeps every value you put in it, repeats included. A set keeps each value once. Adding a value that is already inside changes nothing, so a set is the right tool when you only care about which values exist.
val pets = scala.collection.mutable.Set[String]()
pets += "cat"
pets += "dog"
pets += "cat"
println(pets.size)
println(pets.contains("dog"))
This prints 2 and true: the second "cat" was ignored.
The operations
scala.collection.mutable.Set[String]()makes an empty set of strings.s += xaddsx.s.contains(x)istruewhenxis in the set.s.sizeis the number of values.
Checking contains on a set is fast, even for a big set. A list has to look at its values one by one.
Building a set from a buffer
To drop the repeats of an ArrayBuffer, build a set from it:
val names = scala.collection.mutable.ArrayBuffer("Ava", "Leo", "Ava", "Mia", "Leo")
val kids = scala.collection.mutable.Set[String](names.toSeq: _*)
The set constructor takes its values one by one, and : _* hands the elements of a sequence over that way. names.toSeq turns the buffer into a plain sequence first. Ava and Leo are each in the buffer twice, and the set holds each once, so kids.size is 3.
Mutable versus immutable
Scala's plain Set cannot change. A val holding one does not accept +=, and the code does not compile. Import or write out scala.collection.mutable.Set when you want a set that grows, as in this lesson.
If you only want the number of different values, you don't need a mutable set: for the buffer above, names.size is 5 and names.toSet.size is 3. Here the plain, unchangeable Set is fine, because nothing is added afterwards.
Trying += on a plain Set held in a val gives the compiler error value += is not a member of Set[String].
No order
A set has no order. If you print one, its values can come out in any order, so compare sizes and use contains instead of relying on position.
The exercise
The camp's sign-up list names has some names twice. The task is to build a set kids from it, add Zoe and then Ava, and print Leo is coming if Leo is in the set. The code already prints the size twice.
class Solution {
def mainFunction(): Unit = {
val names = scala.collection.mutable.ArrayBuffer("Ava", "Leo", "Ava", "Mia", "Leo")
val kids = scala.collection.mutable.Set[String](names.toSeq: _*)
println(s"${kids.size} kids")
kids += "Zoe"
kids += "Ava"
println(s"${kids.size} kids")
if (kids.contains("Leo")) {
println("Leo is coming")
}
}
}
The output is:
3 kids
4 kids
Leo is coming
The first count is 3 because Ava and Leo collapsed to one copy each. Adding Zoe makes it 4. Adding Ava again changes nothing.
Common mistakes
- Expecting
+=to report whether the value was new. It just adds. (The methodaddreturnstruefor a new value andfalsefor a repeat, if you need that.) - Using the plain
Setand wondering why+=fails. - Writing
var kidsout of habit. The namekidsalways refers to the same set and the set itself grows, sovalis right. - Expecting the size to go up when you add a value that is already there. Ava's second sign-up leaves the count where it was.
- Counting a buffer's
sizewhen you want the number of different values.
Key takeaways
- A set holds each value once and has no order.
+=adds,containschecks,sizecounts.- Build one from a buffer with
Set[T](buf.toSeq: _*)to remove repeats. - Use
scala.collection.mutable.Setfor a set you change.
