0
0
KotlinComparisonBeginner · 3 min read

MapOf vs mutableMapOf in Kotlin: Key Differences and Usage

mapOf creates an immutable map that cannot be changed after creation, while mutableMapOf creates a mutable map that allows adding, removing, or updating entries. Use mapOf when you want a fixed map and mutableMapOf when you need to modify the map later.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of mapOf and mutableMapOf in Kotlin.

FeaturemapOfmutableMapOf
MutabilityImmutable (read-only)Mutable (modifiable)
ModificationNot allowed after creationAllowed (add, remove, update)
Return TypeMapMutableMap
Use CaseFixed data setsDynamic data sets
PerformanceSlightly faster for read-onlySlightly slower due to mutability support
Syntax ExamplemapOf("a" to 1)mutableMapOf("a" to 1)
⚖️

Key Differences

mapOf returns an immutable Map instance. This means once you create it, you cannot add, remove, or change any key-value pairs. It is useful when you want to ensure the map stays constant throughout your program, which can help avoid bugs related to accidental changes.

On the other hand, mutableMapOf returns a MutableMap, which supports modification operations like put, remove, and updating values by key. This is helpful when your data needs to change dynamically during runtime.

Both functions take pairs of keys and values as arguments, but their returned types differ in mutability. Choosing between them depends on whether you want to allow changes after creation.

⚖️

Code Comparison

Here is how you create and try to modify a map using mapOf. Notice that modification is not allowed.

kotlin
val readOnlyMap = mapOf("apple" to 1, "banana" to 2)
println(readOnlyMap["apple"])
// readOnlyMap["apple"] = 3 // Error: Val cannot be reassigned
// readOnlyMap.put("cherry", 3) // Error: Unresolved reference: put
Output
1
↔️

mutableMapOf Equivalent

Here is the equivalent code using mutableMapOf, which allows modifications.

kotlin
val modifiableMap = mutableMapOf("apple" to 1, "banana" to 2)
println(modifiableMap["apple"])
modifiableMap["apple"] = 3
modifiableMap.put("cherry", 4)
println(modifiableMap)
Output
{apple=3, banana=2, cherry=4}
🎯

When to Use Which

Choose mapOf when you have a fixed set of key-value pairs that should not change, such as configuration constants or lookup tables. This ensures safety and can improve performance by preventing accidental modifications.

Choose mutableMapOf when your map needs to change over time, like collecting user input, caching data, or building a map dynamically. It provides flexibility to add, update, or remove entries as needed.

Key Takeaways

mapOf creates an immutable map that cannot be changed after creation.
mutableMapOf creates a mutable map that supports adding, removing, and updating entries.
Use mapOf for fixed data and mutableMapOf for dynamic data.
Immutable maps help avoid bugs from accidental changes and can be slightly faster.
Mutable maps offer flexibility when your data needs to evolve during program execution.