A Scala Set stores unique values. If the same value is added more than once, it still appears only once.
Creating a Set:
val mySet = Set("b", "c", "c", "d")
// mySet contains b, c, and d
Checking membership:
val exists = mySet.contains("c") // true
Mutable sets:
val mutableSet = scala.collection.mutable.Set("b", "c", "d")
mutableSet += "x"
mutableSet -= "c"
Iterating:
for (value <- mySet) {
println(value)
}
Assignment
Follow the Coding Tutorial and let's play with sets.
Hint
Use Set(...) for unique values.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check