Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   RUBY   
   C++   




CLEAR() - FUNCTION


The clear() method is used to remove all the elements from the Set.


Example :



fun main() {
    val x = mutableSetOf("Mohan", "Kriti", "Salim")
    x.clear()
    println(x)
}


Output :



  []

So, in the above code we have created a Set and initialised to the variable x.


val x = mutableSetOf("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the Set,

java_Collections

Next, we have used the clear() function that removes all the elements from the Set making the Set empty.


And we get an empty Set as output,


[]