0
0
Kotlinprogramming~10 mins

Map creation (mapOf, mutableMapOf) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Map creation (mapOf, mutableMapOf)
Start
Choose map type
Create read-only map
Create changeable map
Add key-value pairs
Use map
End
You start by choosing which map to create: an immutable map with mapOf or a mutable map with mutableMapOf. Then you add key-value pairs and use the map.
Execution Sample
Kotlin
val readOnlyMap = mapOf("a" to 1, "b" to 2)
val changeableMap = mutableMapOf("x" to 10)
changeableMap["y"] = 20
println(readOnlyMap)
println(changeableMap)
Creates an immutable map and a mutable map, adds a new pair to the mutable map, then prints both.
Execution Table
StepActionMap StateOutput
1Create readOnlyMap with pairs ("a" to 1, "b" to 2){"a"=1, "b"=2}
2Create changeableMap with pair ("x" to 10){"x"=10}
3Add pair ("y" to 20) to changeableMap{"x"=10, "y"=20}
4Print readOnlyMap{"a"=1, "b"=2}{a=1, b=2}
5Print changeableMap{"x"=10, "y"=20}{x=10, y=20}
💡 All steps completed, maps created and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
readOnlyMapempty{"a"=1, "b"=2}{"a"=1, "b"=2}{"a"=1, "b"=2}{"a"=1, "b"=2}
changeableMapemptyempty{"x"=10}{"x"=10, "y"=20}{"x"=10, "y"=20}
Key Moments - 2 Insights
Why can't we add new pairs to readOnlyMap after creation?
Because readOnlyMap is created with mapOf which makes an immutable map. The execution_table shows no step where readOnlyMap changes after creation.
How do we add a new key-value pair to changeableMap?
We use the syntax changeableMap["key"] = value as shown in step 3 of the execution_table, which updates the map.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of changeableMap after step 2?
A{"x"=10}
B{"y"=20}
C{"x"=10, "y"=20}
D{}
💡 Hint
Check the 'Map State' column at step 2 in the execution_table.
At which step does changeableMap get a new key-value pair added?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the step where changeableMap changes from {"x"=10} to {"x"=10, "y"=20}.
If we tried to add a new pair to readOnlyMap after step 1, what would happen?
AIt would add the pair successfully.
BIt would silently ignore the addition.
CIt would cause a compile-time error.
DIt would remove an existing pair.
💡 Hint
Recall that mapOf creates an immutable map as shown in the key_moments section.
Concept Snapshot
mapOf creates an immutable (read-only) map.
mutableMapOf creates a mutable (changeable) map.
Add pairs at creation or use [key] = value for mutable maps.
Immutable maps cannot be changed after creation.
Use mutable maps when you need to add or remove entries.
Full Transcript
This example shows how to create maps in Kotlin using mapOf and mutableMapOf. mapOf creates a read-only map that cannot be changed after creation. mutableMapOf creates a map that you can add or change entries in. We create readOnlyMap with two pairs and changeableMap with one pair. Then we add a new pair to changeableMap. Finally, we print both maps to see their contents. The execution table traces each step, showing how the maps change or stay the same. The variable tracker shows the state of each map after each step. Key moments explain why immutable maps cannot be changed and how to add pairs to mutable maps. The quiz tests understanding of these steps and concepts.