Open In App

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



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 myset1: Set[String] = Set("Geeks", "GFG"
                            "GeeksforGeeks", "Geek123")
        val myset2 = Set("C", "C#", "Java", "Scala"
                                          "PHP", "Ruby")
          
        // Display the value of myset1
        println("Set 1:")
        println(myset1)
          
        // Display the value of myset2 using for loop
        println("\nSet 2:")
        for(myset<-myset2)
        {
            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 myset1: Set[String] = Set("Geeks", "GFG"
                            "GeeksforGeeks", "Geek123")
        var myset2 = Set(10, 100, 1000, 10000, 100000)
          
        // Display the value of myset1
        println("Set 1:")
        println(myset1)
          
        // Display the value of myset2 
        // using a foreach loop
        println("\nSet 2:")
        myset2.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

Article Tags :