Skip to content
Related Articles

Related Articles

Improve Article

Scala immutable TreeSet clear() method

  • Last Updated : 19 Apr, 2020

In Scala immutable TreeSet class, the clear() method is utilized to delete all the elements of the TreeSet.

Method Definition: def clear(): Unit

Return Type: It returns an empty TreeSet.

Example #1:




// Scala program of clear() 
// method 
  
// Import TreeSet
import scala.collection.immutable._
  
// 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)
          
    
Output:
Before clear method: TreeSet(1, 2, 3, 4, 5)
After clear method: ()

Example #2:




// Scala program of clear() 
// method 
  
// Import TreeSet
import scala.collection.immutable._
  
// 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)
          
    
Output:
Before clear method: TreeSet(a, e, i, o, u)
After clear method: ()



My Personal Notes arrow_drop_up
Recommended Articles
Page :