📜  带有示例的 Scala TreeSet clear() 方法

📅  最后修改于: 2022-05-13 01:55:32.415000             🧑  作者: Mango

带有示例的 Scala TreeSet clear() 方法

在 Scala TreeSet class中,使用clear()方法删除 TreeSet 的所有元素。

示例 #1:

// Scala program of clear() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet(2, 1, 3, 4, 5) 
          
        // Print the TreeSet
        println("Before clear method: " + t1) 
          
        // Applying clear() method  
        val result = t1.clear
          
        // Displays output 
        println("After clear method: " + result)
          
    } 
} 
输出:
Before clear method: TreeSet(1, 2, 3, 4, 5)
After clear method: ()

示例 #2:

// Scala program of clear() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet("a", "e", "i", "o", "u")
          
        // Print the TreeSet
        println("Before clear method: " + t1) 
          
        // Applying clear() method  
        val result = t1.clear
          
        // Displays output 
        println("After clear method: " + result)
          
    } 
} 
输出:
Before clear method: TreeSet(a, e, i, o, u)
After clear method: ()