Updated March 31, 2023
Introduction to Kotlin Mutable List
The kotlin mutable list is one of the collection interface, and it is used to create the list of datas that can be changed depends on the requirement. The generic collection of the data elements it will be inherited from the collection<T> class; the methods of the MutableList interface supports both readable and writeable functionalities. The elements in the mutable list have been declared, and also it can be performed with collection operations like added more number of elements removable operations being performed by using the remove() method and it has fixed size length mutableListOf() and mutableListOf<E>() for to inert the datas in sequence order using an index.
Syntax of Kotlin Mutable List
In the kotlin language, many classes and interfaces are using with the generic collection types. Among that MutableList is the interface that can be implemented by using the methods like mutuableListOf<Int>(), mutuableListOf<Any>() it can support n number of data types in the interface.
fun main(args:Array<String>)
{
var a: MutuableList<datatype> = mutuableListOf<datatype>()
a.add();
a.set();
a.distinct();
---some logic codes and default methods----
}
The above code is the basic syntax for to use and create the MutuableList interface and its methods like mutuableListOf<datatype>(). Based on that requirement, we can use the default methods like add(), distinct() etc.
How Mutable List Works in Kotlin?
The mutable list interfaces and is one of the generic collections using the data elements. It is one of mutable nature, and it is inherited from the collection<T> class; the methods of the mutablelist interface will support for both read and write functionalities. Once the elements are added and declared in the interface, it tries to add more elements in the memory stack by using the add() method. If we want to perform the user input datas in a unique order, then it will be the fixed-size length, and it is stored in sequence order to insert or store the values in the memory areas.
Each value must have a separate index to retrieve the datas from the backend to the front end. Several methods are used to perform the mutable interface with the separate areas, and it depends on the other child class attribute datas for to perform this operation. It satisfies all the mutable collection operations like arrayListOf(), hashMapOf() and hashSetOf() methods for to achieving these operations with user end.
We can see the simple instance for the ArrayList<T>, which is a default implementation of the List interface. The implementation details should follow with the mutablelist concepts, so it has been abstracted away from the collection class.
Examples of Kotlin Mutable List
Given below are the examples of the Kotlin Mutable List:
Example #1
Code:
package one;
fun main(args: Array<String>){
var ml = mutableListOf<String>()
ml.add("Your name is :Siva")
ml.add("Your name is :Raman")
ml.add("Your name is :Siva Raman")
ml.add("Your name is :Arun")
ml.add("Your name is :Kumar")
ml.add("Your name is :Arun Kumar")
var ma = mutableListOf<String>("Welcome To My Domain","Siva")
var mb = mutableListOf<String>("Welcome To My Domain","Raman")
var mc = mutableListOf<String>("Welcome To My Domain","Siva","Raman")
println("Thank you users your first set of mutable list datas are:")
for(vars in ml){
println(vars)
}
println("Thank you users your second set of mutable list datas are:")
println(ml[2])
ml.add(2,"Vinoth")
println(" We can modify the first set mutable list ml.add(2,\"Vinoth\")")
for(vars in ml){
println(vars)
}
ml.add("Raj")
println("Again we can add the list values ml.add(\"Raj\")")
for(vars in ml){
println(vars)
}
ml.addAll(1,mb)
println("We can add all the list values to single list: ml.addAll(1,mb)")
for(vars in ml){
println(vars)
}
ml.addAll(ma)
println("We can add all the values and make it to the single list: ml.addAll(ma)")
for(vars in ml){
println(vars)
}
ml.remove("Arun")
println("We can remove the specified values: ml.remove(\"Arun\")")
for(vars in ml){
println(vars)
}
ml.removeAt(2)
println("We can remove the specified values using the index: ml.removeAt(2)")
for(vars in ml){
println(vars)
}
ml.removeAll(ma)
println("We can remove the specified values:ml.removeAll(ma)")
for(vars in ml){
println(vars)
}
println("We can remove the specified values: ml.set(2,\"Raman\")")
ml.set(2,"Fazil")
for(vars in ml){
println(vars)
}
println("We can retrieve the specified values: ml.retainAll(mc)")
ml.retainAll(mc)
for(vars in ml){
println(vars)
}
println("We can clear the cache datas: ma.clear()")
ma.clear()
for(vars in ma){
println(vars)
}
println("ma after ma.clear()")
println(ma)
println("We can calculate the sublist datas: ml.subList(1,3)")
println(ml.subList(1,3))
}
Output:
In the above example, we used mutableListOf() methods with operations like add(), remove() method for to add and remove the values by using the index.
Example #2
Code:
package one;
fun main(args: Array<String>){
var ml1 = mutableListOf("January is the first month","February is the second month","March is the third month","April is the fourth month","May is the fifth month","June is the Sixth month","July is the Seventh month","August is the Eigth month","September is the Ninth month","October is the Tenth month","November is the Eleventh month","December is the Twelth month")
ml1.add("Dell")
ml1.add("HCL")
ml1.add("Lenovo")
var ml2 = mutableListOf<String>()
ml2.add("Sony")
ml2.add("Samsung")
ml2.add("Think Pad")
var m23=mutableListOf("Information Technology","Economics","Computer Science","Commerce","Physics","Chemistry","English","Tamil","History","Geography","Digital Signals and System","Mobile Computing","Engineering graphics","Probabaility and Queue Theory","Microprocessor and its Architecture","Software Testing","Cryptography")
m23.add("IT")
m23.add("EC")
m23.add("CS")
m23.add("Com")
m23.add("Phys")
m23.add("Chem")
m23.add("Eng")
m23.add("Tam")
m23.add("His")
m23.add("Geo")
m23.add("DGS")
m23.add("Mob")
m23.add("EG")
m23.add("PQT")
m23.add("Chem")
m23.add("MQP")
m23.add("ST")
m23.add("Cryp")
for(i in ml1){
println(i)
}
println("Thank you users have a nice day")
for(i in ml2){
println(i)
}
for(i in m23){
println(i)
}
}
Output:
In the second example, we used a list method like add() for to adding the values in the memory. Using for loop to iterate the datas and print it on the console.
Example #3
Code:
package one;
fun main(args: Array<String>){
val Homethings = mutableListOf(1,"TV",2,"Fridge",3,"Wasing machine",4,"UPS",5,"Computer",6,"Laptop",7,"mobile",8,"Bureau",9,"Locker",10,"Dry Cleaner")
var jewelleryname = "Lalitha Jewellery,balu jewellery,kumaran jewellery,SJS jewellery, Anbu jewellery, saravana jewellery,kamal and co jewellery,pawn jewellery,kmt jewellery"
println("The list contains the element $jewelleryname or not?" +
" "+Homethings.contains(jewelleryname))
var num = 41
println("The list contains the element $num or not?" +
" "+Homethings.contains(num))
println("The list contains the given elements or not?" +
" "+Homethings.containsAll(setOf(1,3,"Robots")))
var newyears="Tamil New year, Telghu New year,Malyalam New year,Kannada New year,Odisan New year,Marathi New year,Bengali New year,Gujarathi new year"
println(Homethings.contains(newyears))
var machine="Fridge,Washing machine,Chairs,Table,TV,Computer,UPS,AC,Fan,Iron box,Grinder,Mixie"
println(Homethings.contains(machine))
}
Output:
In the final example, we used some mutableListOf() method with additional methods like contains() for to check and validate the datas.
Conclusion
In Mutable List is the interface for performing the operations like creating the instance of the specific class by calling its own constructors. If we want to create the mutable list and it cares about the implementation for to adding and retrieving the datas with some logic conditions specified by the programmer.
Recommended Articles
This is a guide to Kotlin Mutable List. Here we discuss the introduction, syntax and working of mutable list in kotlin along with examples and code implementation. You may also have a look at the following articles to learn more –