0
0
KotlinComparisonBeginner · 3 min read

Mutable vs Immutable Map in Kotlin: Key Differences and Usage

In Kotlin, a Map is immutable by default, meaning you cannot change its contents after creation. A MutableMap allows you to add, remove, or update entries after it is created, providing flexibility to modify the map.
⚖️

Quick Comparison

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

FeatureImmutable Map (Map)Mutable Map (MutableMap)
ModificationNot allowed after creationAllowed (add, remove, update)
InterfaceMapMutableMap extends Map
Methods to changeNo methods like put or removeHas put, remove, clear
Thread safetyGenerally safer as it cannot changeNeeds care if shared across threads
Use caseFixed data, read-only accessDynamic data, needs updates
⚖️

Key Differences

An immutable Map in Kotlin is a collection that holds key-value pairs but does not allow any changes once it is created. You cannot add new entries, remove existing ones, or update values. This makes it safe to share and use when you want to guarantee the data stays the same.

On the other hand, a MutableMap extends the Map interface and adds functions like put(), remove(), and clear(). These allow you to change the map's contents after creation. This flexibility is useful when you need to build or update the map dynamically during program execution.

Because MutableMap can change, it requires more care in multi-threaded environments to avoid unexpected behavior. Immutable maps are simpler to reason about since their data never changes.

⚖️

Code Comparison

Here is how you create and try to modify an immutable map in Kotlin:

kotlin
fun main() {
    val immutableMap: Map<String, Int> = mapOf("apple" to 1, "banana" to 2)
    println("Original immutable map: $immutableMap")
    // The following line would cause a compile error:
    // immutableMap["apple"] = 3
}
Output
Original immutable map: {apple=1, banana=2}
↔️

MutableMap Equivalent

Here is how you create and modify a mutable map in Kotlin:

kotlin
fun main() {
    val mutableMap: MutableMap<String, Int> = mutableMapOf("apple" to 1, "banana" to 2)
    println("Original mutable map: $mutableMap")
    mutableMap["apple"] = 3  // Update value
    mutableMap["cherry"] = 4 // Add new entry
    println("Modified mutable map: $mutableMap")
}
Output
Original mutable map: {apple=1, banana=2} Modified mutable map: {apple=3, banana=2, cherry=4}
🎯

When to Use Which

Choose an immutable Map when your data should not change after creation, such as configuration settings or fixed lookup tables. This ensures safety and simplicity.

Choose a MutableMap when you need to build or update the map dynamically, like caching results or collecting data during program execution. Just be mindful of thread safety if used in concurrent code.

Key Takeaways

Immutable maps (Map) cannot be changed after creation, ensuring data safety.
Mutable maps (MutableMap) allow adding, removing, and updating entries.
Use immutable maps for fixed data and mutable maps for dynamic data.
Mutable maps require care in multi-threaded environments to avoid issues.
Kotlin’s MutableMap extends Map, adding modification methods.