1. Overview
In this article, we will learn the Mutable list in kotlin with examples. The list in Kotlin is an ordered collection of elements and stores the element in a specified order.
You can access the element in the list using its index or position. The index always starts from 0.
2. Mutable list
The Kotlin list is of two types:
- Mutable list
- Read-only list
To create a mutable list, you can use the mutableListOf function as below. You can add, alter or remove the elements from your mutable list.
fun main() { val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") println(mutableList) }
3.1. Add elements to a list
You can use standard library function add to add elements to the mutable list. You can either add to the end of the list or at a specified index.
The syntax of the add function is:
- add(element: E): Boolean – To add an element at the end of the list
- add(index: Int, element: E): Boolean – To add an element at a specified index in the list.
The below code uses both of the above functions. First, the code add("Fig")
adds the element “Fig” to the end of the list. Then add(0, "Apple")
adds the “Apple” at index 0.
fun main() { val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") println(mutableList) mutableList.add("Fig") println(mutableList) mutableList.add(0, "Apple") println(mutableList) }
3.2. Copying elements from collection to a list
You can use standard library function addAll to all elements from a collection to a mutable list. You can either add to the end of the list or at a specified index.
The syntax of the add function is:
- add(elements: Collection<E>): Boolean – To add all the elements from the collection to the end of the list
- add(index: Int, elements: Collection<E>): Boolean – To add all elements at a specified index in the list.
The below code addAll(sourceList)
copies all the elements from sourceList
to the end of the list mutableList
.
fun main() { val sourceList = listOf("Fig", "Guava") val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") mutableList.addAll(sourceList) println(mutableList) }
The below code uses addAll(0, sourceList)
to copy all the elements from sourceList
to mutableList
at index 0.
fun main() { val sourceList = listOf("Fig", "Guava") val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") mutableList.addAll(0, sourceList) println(mutableList) }
3.3. Kotlin list length
You can use the size function to get the number of elements in the list.
fun main() { val readOnlyList = listOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") println(readOnlyList.size) }
3.4. Kotlin empty mutable list
You can use the clear function to remove all elements from the mutable list.
In the below code, we call clear()
to empty the mutable list.
fun main() { val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") println(mutableList) mutableList.clear() println(mutableList) }
3.5. Removing elements from the mutable list
You can use the following standard library functions to remove the elements from a mutable list.
- remove(element: E): Boolean – Removes the specified element from the list
- removeAll(elements: Collection<E>): Boolean – Removes the elements specified in the collection from the mutable list.
- removeAt(index: Int): E – Removes an element at the specified index.
The below code covers all the above functions.
fun main() { val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") println(mutableList) mutableList.remove("Apple") println(mutableList) val toRemoveList = listOf("Banana", "Emu berry") mutableList.removeAll(toRemoveList) println(mutableList) mutableList.removeAt(0) println(mutableList) }
3.6. Get the last index of the mutable list
You can use lastIndex function to get the last index of the mutable list. If the list is empty, then returns -1.
fun main() { val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") println(mutableList.lastIndex) }
3.7. Iterate the elements in the mutable list
You can use the traditional for loop or Iterators to loop through the elements in your mutable list.
The below code uses the iterator to iterate the elements in the list.
fun main() { val mutableList = mutableListOf("Apple", "Banana", "Caimito", "Dragonfruit", "Emu berry") val iterator = mutableList.iterator() while (iterator.hasNext()) { println(iterator.next()) } }
4. Conclusion
To sum up, we have learned the mutable list and various use cases around it.
Pingback: Kotlin get unique elements from list using distinct and distinct by - TedBlob