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




LIST - toMutableList()


toMutableList() is used to copy one List to the other. It creates a Mutable/changeable copy of the existing List.


Example :



fun main() {
    val x = mutableListOf("Mohan", "Kriti", "Salim")
    var y = x.toMutableList()
    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.toMutableList()

java_Collections

And we get the below output,


The Copied List is [Mohan, Kriti, Salim]