Let us see Set in detail
- Set is iterable which contain unique elements (no duplicates)
- As duplicates are not allowed, there is no length (Seq have both length and index)
- Even though we can access data using index, it might not return same value always
- By default Set is immutable, but there is mutable one as well
- Here is the link for official documentation – http://docs.scala-lang.org/overviews/collections/sets
- Let us see how we can use mutable and immutable Set in a demo
- If the Set contain elements for which implicit Ordering defined, we can use SortedSet to sort the data
containsmethod asks whether a set contains a given element. The
applymethod for a set is the same as
set(elem)is the same as
set contains elem. That means sets can also be used as test functions that return true for the elements they contain.
++, which add one or more elements to a set, yielding a new set.
--, which remove one or more elements from a set, yielding a new set.
- Let us create case class Order
- Additions and Removals to mutable sets can be performed using +=, ++=, -= and –=
- Set operations for union, intersection, and set difference. Each of these operations exists in two forms: alphabetic and symbolic. The alphabetic versions are
diff, whereas the symbolic versions are
&~. In fact, the
++that Set inherits from
Traversablecan be seen as yet another alias of
|, except that