Javatpoint Logo
Javatpoint Logo

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

Function of Kotlin MutableList

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

Function 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>): Boolean It adds all the elements of given collection to current collection.
abstract fun clear() 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<E> 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<E>
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.

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:

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.

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<T>() function.

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]

Next TopicKotlin ArrayList




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA