In Scala, an instance of Set
can be declared as follows.
val s = (1 to 6).toSet
Principle difference between sets and sequences are three:
- Sets are unordered
val s = (1 to 6).toSet //> s : scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)
- Sets do not have duplicate elements
s map (_ % 2 ==0) //> res8: scala.collection.immutable.Set[Boolean] = Set(false, true)
- The fundamental operation on set is
contains
.
s contains 5 //> res9: Boolean = true