Industrial Training




Kotlin MutableList (mutableListOf())


Kotlin MutableList is an interface and generic collection of elements. MutableList interface is mutable in nature. It inherits form Collection< T> class. The methods of MutableList interface supports both read and write functionalities. Once the elements in MutableList have declared, it can be added more elements in it or removed, so it has no fixed size length. To use the MutableList interface we use its function called mutableListOf() or mutableListOf< E>(). The elements of MutableList follow the sequence of insertion order and contains index number same as array.


MutableList Interface Declaration


interface MutableList< E> : List< E>, MutableCollection< E> (source)  
interface MutableList< E> : List< E>, MutableCollection< E> (source)

Function of Kotlin MutableList


There are several methods are available in MutableList interface. Some methods of MutableList interface are mention below.


Functions Descriptions
abstract fun add(element: E): Boolean

It adds the given element to the collection.

abstract fun add(index: Int, element: E)

It adds the element at specified index.

abstract fun addAll(elements: Collection< E>): Boolea

It adds all the elements of given collection to current collection.

abstract fun clear()nt

It removes all the elements from this collection.

abstract fun listIterator(): MutableListIterator< E>

It returns a list iterator over the elements in proper sequence in current list.

abstract fun listIterator(index: Int): MutableListIterator

It returns a list iterator starting from specified index over the elements in list in proper sequence.

abstract fun remove(element: E): Boolean

It removes the specified element if it present in current collection.

abstract fun removeAll(elements: Collection< E>): Boolean

It removes all the elements from the current list which are also present in the specified collection.

abstract fun removeAt(index: Int): E

It removes element at given index from the list.

abstract fun retainAll(elements: Collection< E>): Boolean

It retains all the elements within the current collection which are present in given collection.

abstract operator fun set(index: Int, element: E): E

It replaces the element and add new at given index with specified element.

abstract fun subList( fromIndex: Int, toIndex: Int ): MutableList

It returns part of list from specified fromIndex (inclusive) to toIndex (exclusive) from current list. The returned list is backed by current list, so non-structural changes in the returned list are reflected in current list, and vice-versa.


Kotlin MutableList Example 1


Let's see an example of MutableList using mutableListOf() function and traverse its elements.


fun main(args: Array< String>){  
    var mutableList = mutableListOf("Ajay","Vijay","Prakash","Vijay")  
  
    for(element in mutableList){  
        println(element)  
    }  
    println()  
    for(index in 0..mutableList.size-1){  
        println(mutableList[index])  
    }  
}  
fun main(args: Array< String>){  
    var mutableList = mutableListOf("Ajay","Vijay","Prakash","Vijay")  
  
    for(element in mutableList){  
        println(element)  
    }  
    println()  
    for(index in 0..mutableList.size-1){  
        println(mutableList[index])  
    }  
}

Output:
Ajay
Vijay
Prakash
Vijay

Ajay
Vijay
Prakash
Vijay

Kotlin MutableList Example 2


The function mutableListOf() of MutableList interface provides facilities to add elements after its declaration. MutableList can also be declared as empty and added elements later but in this situation we need to define its generic type. For example:


  
fun main(args: Array< String>){  
    var mutableList1 = mutableListOf("Ajay","Vijay")  
    mutableList1.add("Prakash")  
    mutableList1.add("Vijay")  
  
    var mutableList2 = mutableListOf< String>()  
    mutableList2.add("Ajeet")  
    mutableList2.add("Amit")  
    mutableList2.add("Akash")  
  
    for(element in mutableList1){  
        println(element)  
    }  
    println()  
    for(element in mutableList2){  
        println(element)  
    }  
}  
fun main(args: Array< String>){  
    var mutableList1 = mutableListOf("Ajay","Vijay")  
    mutableList1.add("Prakash")  
    mutableList1.add("Vijay")  
  
    var mutableList2 = mutableListOf< String>()  
    mutableList2.add("Ajeet")  
    mutableList2.add("Amit")  
    mutableList2.add("Akash")  
  
    for(element in mutableList1){  
        println(element)  
    }  
    println()  
    for(element in mutableList2){  
        println(element)  
    }  
} 

Output:
Ajay
Vijay
Prakash
Vijay

Ajeet
Amit
Akash

Kotlin MutableList Example 3


For more specific we can provide the generic types of MutableList interface such as mutableListOf< Int>(), mutableListOf< String>(), mutableListOf< Any>(). The mutableListOf< Int>() takes only integer value, mutableListOf< String>() takes only String value and mutableListOf< Any>() takes different data types value at the same time. Let's see the example.


  
fun main(args: Array< String>){  
    var mutableListInt: MutableList< Int> = mutableListOf< Int>()  
    var mutableListString: MutableList< String> = mutableListOf< String>()  
    var mutableListAny: MutableList< Any> = mutableListOf< Any>()  
  
    mutableListInt.add(5)  
    mutableListInt.add(7)  
    mutableListInt.add(10)  
    mutableListInt.add(2,15) //add element 15 at index 2  
  
    mutableListString.add("Ajeet")  
    mutableListString.add("Ashu")  
    mutableListString.add("Mohan")  
  
    mutableListAny.add("Sunil")  
    mutableListAny.add(2)  
    mutableListAny.add(5)  
    mutableListAny.add("Raj")  
  
    println(".....print Int type.....")  
    for(element in mutableListInt){  
        println(element)  
    }  
    println()  
    println(".....print String type.....")  
    for(element in mutableListString){  
        println(element)  
    }  
    println()  
    println(".....print Any type.....")  
    for(element in mutableListAny){  
        println(element)  
    }  
}  
fun main(args: Array< String>){  
    var mutableListInt: MutableList< Int> = mutableListOf< Int>()  
    var mutableListString: MutableList< String> = mutableListOf< String>()  
    var mutableListAny: MutableList< Any> = mutableListOf< Any>()  
  
    mutableListInt.add(5)  
    mutableListInt.add(7)  
    mutableListInt.add(10)  
    mutableListInt.add(2,15) //add element 15 at index 2  
  
    mutableListString.add("Ajeet")  
    mutableListString.add("Ashu")  
    mutableListString.add("Mohan")  
  
    mutableListAny.add("Sunil")  
    mutableListAny.add(2)  
    mutableListAny.add(5)  
    mutableListAny.add("Raj")  
  
    println(".....print Int type.....")  
    for(element in mutableListInt){  
        println(element)  
    }  
    println()  
    println(".....print String type.....")  
    for(element in mutableListString){  
        println(element)  
    }  
    println()  
    println(".....print Any type.....")  
    for(element in mutableListAny){  
        println(element)  
    }  
} 

Output:
.....print Int type.....
5
7
15
10

.....print String type.....
Ajeet
Ashu
Mohan

.....print Any type.....
Sunil
2
5
Raj

Kotlin MutableList Example 4


Let's see the use of different function of MutableList interface using mutableListOf() function.


  
fun main(args: Array< String>){  
    var mutableList = mutableListOf< String>()  
  
    mutableList.add("Ajay") // index 0  
    mutableList.add("Vijay") // index 1  
    mutableList.add("Prakash") // index 2  
  
    var mutableList2 = mutableListOf< String>("Rohan","Raj")  
    var mutableList3 = mutableListOf< String>("Dharmesh","Umesh")  
    var mutableList4 = mutableListOf< String>("Ajay","Dharmesh","Ashu")  
  
    println(".....mutableList.....")  
    for(element in mutableList){  
        println(element)  
    }  
    println(".....mutableList[2].....")  
    println(mutableList[2])  
    mutableList.add(2,"Rohan")  
    println("......mutableList.add(2,\"Rohan\")......")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.add("Ashu")  
    println(".....mutableList.add(\"Ashu\")......")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.addAll(1,mutableList3)  
    println("... mutableList.addAll(1,mutableList3)....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.addAll(mutableList2)  
    println("...mutableList.addAll(mutableList2)....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.remove("Vijay")  
    println("...mutableList.remove(\"Vijay\")....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.removeAt(2)  
    println("....mutableList.removeAt(2)....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.removeAll(mutableList2)  
    println("....  mutableList.removeAll(mutableList2)....")  
    for(element in mutableList){  
        println(element)  
    }  
  
    println("....mutableList.set(2,\"Ashok\")....")  
    mutableList.set(2,"Ashok")  
    for(element in mutableList){  
        println(element)  
    }  
  
    println(".... mutableList.retainAll(mutableList4)....")  
    mutableList.retainAll(mutableList4)  
    for(element in mutableList){  
        println(element)  
    }  
    println(".... mutableList2.clear()....")  
    mutableList2.clear()  
  
    for(element in mutableList2){  
        println(element)  
    }  
    println(".... mutableList2 after mutableList2.clear()....")  
    println(mutableList2)  
  
    println("....mutableList.subList(1,2)....")  
    println(mutableList.subList(1,2))  
  
}  
fun main(args: Array< String>){  
    var mutableList = mutableListOf< String>()  
  
    mutableList.add("Ajay") // index 0  
    mutableList.add("Vijay") // index 1  
    mutableList.add("Prakash") // index 2  
  
    var mutableList2 = mutableListOf< String>("Rohan","Raj")  
    var mutableList3 = mutableListOf< String>("Dharmesh","Umesh")  
    var mutableList4 = mutableListOf< String>("Ajay","Dharmesh","Ashu")  
  
    println(".....mutableList.....")  
    for(element in mutableList){  
        println(element)  
    }  
    println(".....mutableList[2].....")  
    println(mutableList[2])  
    mutableList.add(2,"Rohan")  
    println("......mutableList.add(2,\"Rohan\")......")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.add("Ashu")  
    println(".....mutableList.add(\"Ashu\")......")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.addAll(1,mutableList3)  
    println("... mutableList.addAll(1,mutableList3)....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.addAll(mutableList2)  
    println("...mutableList.addAll(mutableList2)....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.remove("Vijay")  
    println("...mutableList.remove(\"Vijay\")....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.removeAt(2)  
    println("....mutableList.removeAt(2)....")  
    for(element in mutableList){  
        println(element)  
    }  
    mutableList.removeAll(mutableList2)  
    println("....  mutableList.removeAll(mutableList2)....")  
    for(element in mutableList){  
        println(element)  
    }  
  
    println("....mutableList.set(2,\"Ashok\")....")  
    mutableList.set(2,"Ashok")  
    for(element in mutableList){  
        println(element)  
    }  
  
    println(".... mutableList.retainAll(mutableList4)....")  
    mutableList.retainAll(mutableList4)  
    for(element in mutableList){  
        println(element)  
    }  
    println(".... mutableList2.clear()....")  
    mutableList2.clear()  
  
    for(element in mutableList2){  
        println(element)  
    }  
    println(".... mutableList2 after mutableList2.clear()....")  
    println(mutableList2)  
  
    println("....mutableList.subList(1,2)....")  
    println(mutableList.subList(1,2))  
  
} 

Output:
.....mutableList.....
Ajay
Vijay
Prakash
.....mutableList[2].....
Prakash
......mutableList.add(2,"Rohan")......
Ajay
Vijay
Rohan
Prakash
.....mutableList.add("Ashu")......
Ajay
Vijay
Rohan
Prakash
Ashu
... mutableList.addAll(1,mutableList3)....
Ajay
Dharmesh
Umesh
Vijay
Rohan
Prakash
Ashu
...mutableList.addAll(mutableList2)....
Ajay
Dharmesh
Umesh
Vijay
Rohan
Prakash
Ashu
Rohan
Raj
...mutableList.remove("Vijay")....
Ajay
Dharmesh
Umesh
Rohan
Prakash
Ashu
Rohan
Raj
....mutableList.removeAt(2)....
Ajay
Dharmesh
Rohan
Prakash
Ashu
Rohan
Raj
....  mutableList.removeAll(mutableList2)....
Ajay
Dharmesh
Prakash
Ashu
....mutableList.set(2,"Ashok")....
Ajay
Dharmesh
Ashok
Ashu
.... mutableList.retainAll(mutableList4)....
Ajay
Dharmesh
Ashu
.... mutableList2.clear()....
.... mutableList2 after mutableList2.clear()....
[]
....mutableList.subList(1,2)....
[Dharmesh]


Hi I am Pluto.