Scala TreeSet

Scala TreeSet

Set是一种数据结构,它允许我们存储唯一的元素。元素的排序并不是由Set来保证的,TreeSet会按照给定的顺序排列元素。在Scala中,TreeSet有两个版本:scala.collection.immutable.TreeSet和scala.collection.mutable.TreeSet。

语法

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

对TreeSet进行的操作

初始化一个TreeSet:

下面是创建或初始化TreeSet的例子。
例子

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

输出

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

检查TreeSet中的特定元素:
例子

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

输出

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

在TreeSet中添加一个元素:

我们可以通过使用 + 号在TreeSet中添加一个元素。下面是在TreeSet中添加一个元素的例子。
例子

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

输出

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

在TreeSets中添加两个TreeSets。

我们可以通过使用++号添加两个TreeSets。下面是添加两个TreeSets的例子。
例子

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

输出

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

删除TreeSet中的元素。

我们可以通过使用-号来移除TreeSet中的一个元素。下面是移除TreeSet中一个元素的例子。

例子

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

输出

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

寻找两个TreeSets之间的交集。

我们可以通过使用&符号找到两个树集的交集。下面是寻找两个树集交集的例子。

例子

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

输出

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)

初始化一个空的TreeSet :

下面是显示空TreeSet的例子。

例子

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

输出

Empty TreeSet = TreeSet()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程