July 2017
Intermediate to advanced
796 pages
18h 55m
English
A set is one of the most widely used collections. In sets order will not be preserved and sets don't allow duplicate elements. You can think of it as the mathematical notation of sets. Let's demonstrate this by an example, and we will see how sets don't preserve ordering and don't allow duplicates:
scala> val numbers = Set( 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)numbers: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
The following source code shows the different uses of sets in a Scala program:
package com.chapter4.CollectionAPIobject SetExample { def main(args: Array[String]) { // Empty set of integer type var sInteger : Set[Int] = Set() // Set of even numbers var sEven : Set[Int] = Set(2,4,8,10) //Or you can use this syntax var sEven2 ...Read now
Unlock full access