Start learning free

LESSON 361 OF 677 · HASH TABLE

Hash Sets in Scala

Lesson

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.


STEP 1 OF 6
    Ctrl + Enter to check