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




LIST - toList()


toList() is used to copy one List to the other. It creates a Read Only copy of the existing List


Example :



fun main() {
    val x = mutableListOf("Mohan", "Kriti", "Salim")
    var y = x.toList()
    println("The Copied List is "+y)
} 


Output :



 The Copied List is [Mohan, Kriti, Salim]

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


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

Below is how the values are positioned in the List,


java_Collections

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


Then assign it to 'y'.


var y = x.toList()

java_Collections

And we get the below output,


The Copied List is [Mohan, Kriti, Salim]