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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
println( "Initialize a 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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
println( "Initialize a TreeSet" )
val treeSet : TreeSet[String] = TreeSet( "Geeks" ,
"GeeksForGeeks" , "Author" )
println(s "Elements are = $treeSet" )
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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
println( "Initialize a TreeSet" )
val ts : TreeSet[String] = TreeSet( "Geeks" ,
"GeeksForGeeks" , "Author" )
println(s "Elements are = $ts" )
val ts 1 : 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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
println( "Initialize a TreeSet" )
val ts : TreeSet[String] = TreeSet( "Geeks" ,
"GeeksForGeeks" , "Author" )
println(s "Elements are = $ts" )
val ts 1 : 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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
println( "Initialize a TreeSet" )
val ts : TreeSet[String] = TreeSet( "Geeks" ,
"GeeksForGeeks" , "Author" )
println(s "Elements are = $ts" )
val ts 1 : 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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
println( "Initialize two TreeSets" )
val ts : TreeSet[String] = TreeSet( "Geeks" ,
"GeeksForGeeks" , "Author" )
println(s "Elements of treeset1 are = $ts" )
val ts 1 : TreeSet[String] = TreeSet( "Java" ,
"Geeks" , "Scala" )
println(s "Elements of treeset2 are = $ts1" )
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:
import scala.collection.immutable.TreeSet
object GFG
{
def main(args : Array[String])
{
val emptyTreeSet : TreeSet[String] = TreeSet.empty[String]
println(s "Empty TreeSet = $emptyTreeSet" )
}
}
|
Output:
Empty TreeSet = TreeSet()