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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
        }
    }
}

chevron_right


Output:

Set 1:
Set(Geeks, GFG, GeeksforGeeks, Geek123)

Set 2:
Scala
C#
Ruby
PHP
C
Java

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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))
    }
}

chevron_right


Output:

Set 1:
Set(Geeks, GFG, GeeksforGeeks, Geek123)

Set 2:
10
100000
10000
1000
100

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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))
    }
}

chevron_right


Output:

0
3
6
7
8
45
56
87


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.