0
0
KotlinComparisonBeginner · 3 min read

Mutable vs Immutable List in Kotlin: Key Differences and Usage

In Kotlin, a List is immutable, meaning you cannot change its elements after creation. A MutableList allows you to add, remove, or update elements, making it changeable during runtime.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of immutable List and mutable MutableList in Kotlin.

FeatureImmutable List (List)Mutable List (MutableList)
Can elements be changed?No, elements cannot be added, removed, or updatedYes, elements can be added, removed, or updated
Methods to modify listNo modifying methods availableSupports methods like add(), remove(), set()
Thread safetyGenerally safer as it cannot changeNeeds care if accessed from multiple threads
Use caseWhen you want a fixed collection of itemsWhen you need to change the list dynamically
Declaration exampleval list: List<Int> = listOf(1, 2, 3)val list: MutableList<Int> = mutableListOf(1, 2, 3)
⚖️

Key Differences

The main difference between List and MutableList in Kotlin is mutability. An immutable List does not allow any changes after it is created. You cannot add, remove, or update elements. This makes it safer to share across different parts of your program without worrying about accidental changes.

On the other hand, a MutableList lets you modify the list after creation. You can add new elements, remove existing ones, or change elements at specific positions. This flexibility is useful when you need a collection that changes over time, like a list of tasks that can grow or shrink.

Both types share many common methods for reading elements, like get() or iteration, but only MutableList has methods to change the list. Kotlin encourages using immutable lists by default for safer and clearer code, switching to mutable lists only when necessary.

⚖️

Code Comparison

Here is how you create and try to modify an immutable List in Kotlin. Notice that modification methods are not available.

kotlin
fun main() {
    val immutableList: List<Int> = listOf(1, 2, 3)
    println("Original immutable list: $immutableList")
    // The following line would cause a compile error:
    // immutableList.add(4)
}
Output
Original immutable list: [1, 2, 3]
↔️

MutableList Equivalent

Here is how you create and modify a MutableList in Kotlin. You can add, remove, and update elements freely.

kotlin
fun main() {
    val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)
    println("Original mutable list: $mutableList")
    mutableList.add(4)
    mutableList[0] = 10
    println("Modified mutable list: $mutableList")
}
Output
Original mutable list: [1, 2, 3] Modified mutable list: [10, 2, 3, 4]
🎯

When to Use Which

Choose immutable List when you want a fixed collection that should not change after creation. This helps prevent bugs and makes your code easier to understand and maintain.

Choose mutable MutableList when you need to add, remove, or update elements dynamically during program execution, such as managing a list of user inputs or live data.

As a good practice, prefer immutable lists by default and switch to mutable lists only when you have a clear need to modify the collection.

Key Takeaways

Immutable List cannot be changed after creation, making it safer and simpler.
Mutable MutableList allows adding, removing, and updating elements dynamically.
Use immutable lists by default for safer code and mutable lists only when modification is needed.
Both types support reading elements the same way, but only mutable lists support modification methods.
Choosing the right list type improves code clarity and reduces bugs.