Set in Scala | Set-1
A set is a collection which only contains unique items. The uniqueness of a set are defined by the == method of the type that set holds. If you try to add a duplicate item in the set, then set quietly discard your request.
Syntax:
// Immutable set val variable_name: Set[type] = Set(item1, item2, item3) or val variable_name = Set(item1, item2, item3) // Mutable Set var variable_name: Set[type] = Set(item1, item2, item3) or var variable_name = Set(item1, item2, item3)
Some Important Points about Set in Scala
- In Scala, both mutable and immutable sets are available. Mutable set is those set in which the value of the object is change but, in the immutable set, the value of the object is not changed itself.
- By default set in Scala are immutable.
- In Scala, the immutable set is defined under Scala.collection.immutable._ package and mutable set are defined under Scala.collection.mutable._ package.
- We can also define a mutable set under Scala.collection.immutable._ package as shown in the below example.
- A Set has various methods to add, remove clear, size, etc. to enhance the usage of the set.
- In Scala, We are allowed to create empty set.
Syntax:// Immutable empty set val variable_name = Set() // Mutable empty set var variable_name = Set()
Example 1:
// Scala program to illustrate the // use of immutable set import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating and initializing immutable sets val myset 1 : Set[String] = Set( "Geeks" , "GFG" , "GeeksforGeeks" , "Geek123" ) val myset 2 = Set( "C" , "C#" , "Java" , "Scala" , "PHP" , "Ruby" ) // Display the value of myset1 println( "Set 1:" ) println(myset 1 ) // Display the value of myset2 using for loop println( "\nSet 2:" ) for (myset < -myset 2 ) { println(myset) } } } |
Output:
Set 1: Set(Geeks, GFG, GeeksforGeeks, Geek123) Set 2: Scala C# Ruby PHP C Java
Example 2:
// Scala program to illustrate the // use of mutable set import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating and initializing mutable sets var myset 1 : Set[String] = Set( "Geeks" , "GFG" , "GeeksforGeeks" , "Geek123" ) var myset 2 = Set( 10 , 100 , 1000 , 10000 , 100000 ) // Display the value of myset1 println( "Set 1:" ) println(myset 1 ) // Display the value of myset2 // using a foreach loop println( "\nSet 2:" ) myset 2 .foreach((item : Int) => println(item)) } } |
Output:
Set 1: Set(Geeks, GFG, GeeksforGeeks, Geek123) Set 2: 10 100000 10000 1000 100
Example 3:
// Scala program to illustrate the // use of empty set import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating empty sets val myset = Set() // Display the value of myset println( "The empty set is:" ) println(myset) } } |
Output:
The empty set is: Set()
Sorted Set
In Set, SortedSet is used to get values from the set in sorted order. SortedSet is only work for immutable set.
Example:
// Scala program to get sorted values // from the set import scala.collection.immutable.SortedSet object Main { def main(args : Array[String]) { // Using SortedSet to get sorted values val myset : SortedSet[Int] = SortedSet( 87 , 0 , 3 , 45 , 7 , 56 , 8 , 6 ) myset.foreach((items : Int) => println(items)) } } |
Output:
0 3 6 7 8 45 56 87
Please Login to comment...