— Kotlin, Lists, Mutable Lists — 2 min read
When working with collections in Kotlin, it is important to understand the distinction between List and MutableList. Both types are used to store multiple elements, but they differ in terms of immutability. In this article, we will delve into the characteristics and usage scenarios of these two types to help you make informed decisions when working with collections in your Android development projects.
The List interface represents an immutable collection of elements in Kotlin. Once initialized, a List cannot be modified by adding or removing elements. It provides read-only access to its elements and supports operations like accessing elements by index, checking for element existence, and iterating over the collection.
Here's an example of creating and using a List in Kotlin:
1val fruits = listOf("Apple", "Orange", "Banana")2println(fruits[0]) // Output: Apple3println(fruits.contains("Orange")) // Output: true4for (fruit in fruits) {5 println(fruit)6}In this example, we create a List of fruits and perform various operations such as accessing an element by index (fruits[0]), checking if the list contains a specific element (fruits.contains("Orange")), and iterating over the elements using a loop.
It's important to note that since List is immutable, attempting to add or remove elements will result in a compilation error.
On the other hand, the MutableList interface represents a modifiable collection of elements. Unlike List, a MutableList allows adding, removing, and modifying elements after initialization. It provides both read and write access to its elements and supports operations such as adding elements, removing elements, updating elements at a specific index, and more.
Let's take a look at an example of using a MutableList in Kotlin:
1val numbers = mutableListOf(1, 2, 3)2numbers.add(4)3numbers.remove(2)4numbers[0] = 55
6println(numbers) // Output: [5, 3, 4]In this example, we create a MutableList of numbers and perform various operations such as adding an element (numbers.add(4)), removing an element (numbers.remove(2)), and updating an element at index 0 (numbers[0] = 5).
As you can see, MutableList provides the flexibility to modify the collection based on your application's requirements.
The decision to use either List or MutableList depends on whether you need an immutable or mutable collection in your codebase.
Use List when:
Use MutableList when:
It's worth mentioning that if you receive a List from an external source but need to modify it, you can convert it to a MutableList using the toMutableList() function.
1val immutableList = listOf(1, 2, 3)2val mutableList = immutableList.toMutableList()3mutableList.add(4)4
5println(mutableList) // Output: [1, 2, 3, 4]In summary, understanding the difference between List and MutableList is crucial when working with collections in Kotlin. While List provides an immutable collection that cannot be modified after initialization, MutableList offers flexibility by allowing addition, removal, and modification of elements. Choosing the appropriate type ensures your code aligns with your specific requirements and helps maintain clarity and correctness in your Android applications.