A set represents an unordered collection of distinct elements.
Scala provides both immutable and mutable sets. By default, Set is equal to scala.collection.immutable.Set.
You can create sets using the Set constructor, or by converting any other collection to a Set using its toSet method:
scala> var s = Set(2, 3, 4, 5, 7) s: scala.collection.immutable.Set[Int] = Set(5, 2, 7, 3, 4) scala> var s1 = Set[Int]() s1: scala.collection.immutable.Set[Int] = Set() scala> val a = Array(7, 4, 5, 3, 4, 2, 7, 4) a: Array[Int] = Array(7, 4, 5, 3, 4, 2, 7, 4) scala> var s2 = a.toSet s2: scala.collection.immutable.Set[Int] = Set(5, 2, 7, 3, 4) scala> s == s2 res0: Boolean = true
Important Set methods are:
scala.collection.mutable.Set provides mutable sets. In addition to the methods above, mutable sets provide += and -= operators: