Scala TreeSet clear() method with example

In Scala 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Before clear method: TreeSet(1, 2, 3, 4, 5)
After clear method: ()

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Before clear method: TreeSet(a, e, i, o, u)
After clear method: ()



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.