Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

TreeSet in Scala

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:




// 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"
    


Output:

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

 
Check specific elements in TreeSet :
Example:




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


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:




// 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"
    


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:




// 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"
    


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:




// 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"
    


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:




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


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:




// 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"
    


Output:

Empty TreeSet = TreeSet()

My Personal Notes arrow_drop_up
Last Updated : 09 Aug, 2019
Like Article
Save Article
Similar Reads