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




TOSET() - FUNCTION


The toSet() method is used to copy one Set to the other creating a read only set.


Example :



fun main() {
    val x = mutableSetOf("Mohan", "Kriti", "Salim")
    var y = x.toSet()
    println("The Copied Set is "+y)
}


Output :



  The Copied Set is [Mohan, Kriti, Salim]

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

Then we have used the toSet() method and create a new Set that would be the exact copy of x.


Then assign it to y.


	var y = x.toSet()
java_Collections


And we get the below output,


The Copied Set is [Mohan, Kriti, Salim]