TreeSet in Scala

Set is a data structure which allows us to store elements which are unique. The ordering of elements does not guarantee by the Set, than a TreeSet will make elements in a given order. In Scala, TreeSet have two versions: scala.collection.immutable.TreeSet and scala.collection.mutable.TreeSet.

Syntax:

var TreesetName = TreeSet(element1, element2, element3, ....) 

Operations perform with TreeSet

Initialize a TreeSet :

Below is the example to create or initialize TreeSet.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Initializing TreeSet 
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initialize a TreeSet"
          
        // Creating TreeSet 
        val treeSet: TreeSet[String] = TreeSet("Geeks"
                                    "GeeksForGeeks", "Author"
        println(s"Elements are = $treeSet"
    

chevron_right


Output:

Initialize a TreeSet
Elements are = TreeSet(Author, Geeks, GeeksForGeeks)

 
Check specific elements in TreeSet :
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Check specific elements in TreeSet 
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initialize a TreeSet"
          
        // Creating TreeSet 
        val treeSet: TreeSet[String] = TreeSet("Geeks"
                                "GeeksForGeeks", "Author"
        println(s"Elements are = $treeSet"
          
        // Checking 
        println(s"Element Geeks = ${treeSet("Geeks")}"
        println(s"Element Student = ${treeSet("Student")}"
    

chevron_right


Output:

Initialize a TreeSet
Elements are = TreeSet(Author, Geeks, GeeksForGeeks)
Element Geeks = true
Element Student = false

 
Adding an element in TreeSet :

We can add an element in TreeSet by using + sign. below is the example of adding an element in TreeSet.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of adding an element in TreeSet 
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initialize a TreeSet"
          
        // Creating TreeSet 
        val ts: TreeSet[String] = TreeSet("Geeks"
                            "GeeksForGeeks", "Author"
        println(s"Elements are = $ts"
          
        // Adding an element in HashSet 
        val ts1: TreeSet[String] = ts + "GeeksClasses"
        println(s"Adding elements to TreeSet = $ts1"
    

chevron_right


Output:

Initialize a TreeSet
Elements are = TreeSet(Author, Geeks, GeeksForGeeks)
Adding elements to TreeSet = TreeSet(Author, Geeks, GeeksClasses, GeeksForGeeks)

 
Adding two TreeSets in TreeSets :

We can add two TreeSets by using ++ sign. below is the example of adding two TreeSets.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of adding two TreeSets
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initialize a TreeSet"
          
        // Creating HashSet 
        val ts: TreeSet[String] = TreeSet("Geeks"
                                "GeeksForGeeks", "Author"
        println(s"Elements are = $ts"
          
        // Adding elements in HashSet 
        val ts1: TreeSet[String] = ts ++ TreeSet[String]("Java", "Scala"
        println(s"Add more than one TreeSets = $ts1"
    

chevron_right


Output:

Initialize a TreeSet
Elements are = TreeSet(Author, Geeks, GeeksForGeeks)
Add more than one TreeSets = TreeSet(Author, Geeks, GeeksForGeeks, Java, Scala)

 
Remove element in TreeSet :

We can remove an element in TreeSet by using – sign. below is the example of removing an element in TreeSet.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of removing element in TreeSet 
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initialize a TreeSet"
          
        // Creating HashSet 
        val ts: TreeSet[String] = TreeSet("Geeks"
                                "GeeksForGeeks", "Author"
        println(s"Elements are = $ts"
          
        // removing elements in HashSet 
        val ts1: TreeSet[String] = ts - "Geeks"
        println(s"remove element from treeset = $ts1"
    

chevron_right


Output:

Initialize a TreeSet
Elements are = TreeSet(Author, Geeks, GeeksForGeeks)
remove element from treeset = TreeSet(Author, GeeksForGeeks)

 
Find the intersection between two TreeSets :

We can find intersection between two TreeSets by using & sign. below is the example of finding intersection between two TreeSets.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of finding the intersection
// between two TreeSets 
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initialize two TreeSets"
          
        // Creating two TreeSet 
        val ts: TreeSet[String] = TreeSet("Geeks"
                            "GeeksForGeeks", "Author"
        println(s"Elements of treeset1 are = $ts"
          
        val ts1: TreeSet[String] = TreeSet("Java"
                                    "Geeks", "Scala"
        println(s"Elements of treeset2 are = $ts1"
          
        // finding the intersection between two TreeSets 
        println(s"Intersection of treeSet1 and treeSet2 = ${ts & ts1}"
    

chevron_right


Output:

Initialize two TreeSets
Elements of treeset1 are = TreeSet(Author, Geeks, GeeksForGeeks)
Elements of treeset2 are = TreeSet(Geeks, Java, Scala)
Intersection of treeSet1 and treeSet2 = TreeSet(Geeks)

 

Initializing an empty TreeSet :

Below is the example to show empty TreeSet.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Initializing an empty TreeSet 
import scala.collection.immutable.TreeSet 
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        // Initializing an empty TreeSet 
        val emptyTreeSet: TreeSet[String] = TreeSet.empty[String] 
        println(s"Empty TreeSet = $emptyTreeSet"
    

chevron_right


Output:

Empty TreeSet = TreeSet()


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.