Create and Use Maps with mapOf and mutableMapOf in Kotlin
📖 Scenario: You are organizing a small library. You want to keep track of book titles and their authors using a map.
🎯 Goal: Build a Kotlin program that creates a map of books and authors, adds a new book, and then prints all entries.
📋 What You'll Learn
Create an immutable map using mapOf with given book-author pairs
Create a mutable map using mutableMapOf from the immutable map
Add a new book and author to the mutable map
Print all book titles and authors from the mutable map
💡 Why This Matters
🌍 Real World
Maps are useful to store pairs of related data, like book titles and authors, product names and prices, or usernames and emails.
💼 Career
Understanding how to create and manipulate maps is important for many programming jobs, especially when working with data collections and key-value pairs.
Progress0 / 4 steps
1
Create an immutable map of books and authors
Create an immutable map called books using mapOf with these exact entries: "1984" to "George Orwell", "Brave New World" to "Aldous Huxley", and "Fahrenheit 451" to "Ray Bradbury".
Kotlin
Hint
Use mapOf with pairs like "key" to "value" inside parentheses.
2
Create a mutable map from the immutable map
Create a mutable map called mutableBooks using mutableMapOf and initialize it with the entries from the books map.
Kotlin
Hint
Create an empty mutable map with mutableMapOf<String, String>() and then add all entries from books using putAll.
3
Add a new book and author to the mutable map
Add a new entry to mutableBooks with the key "The Hobbit" and the value "J.R.R. Tolkien".
Kotlin
Hint
Use the bracket notation mutableBooks["The Hobbit"] = "J.R.R. Tolkien" to add a new entry.
4
Print all book titles and authors
Use a for loop with variables title and author to iterate over mutableBooks.entries and print each book and author in the format: "Title: [title], Author: [author]".
Kotlin
Hint
Use a for loop with for ((title, author) in mutableBooks.entries) and print with println("Title: $title, Author: $author").
Practice
(1/5)
1. What does mapOf create in Kotlin?
easy
A. A list of values without keys
B. A mutable map that can be changed later
C. A fixed map that cannot be changed after creation
D. An empty map with no keys or values
Solution
Step 1: Understand mapOf purpose
mapOf creates a map with key-value pairs that cannot be modified after creation.
Step 2: Compare with other map types
mutableMapOf allows changes, but mapOf does not.
Final Answer:
A fixed map that cannot be changed after creation -> Option C
2. Which of the following is the correct syntax to create a mutable map with keys "a" and "b" and values 1 and 2 respectively?
easy
A. val map = mutableMapOf("a" to 1, "b" to 2)
B. val map = mapOf("a" -> 1, "b" -> 2)
C. val map = mutableMapOf("a" = 1, "b" = 2)
D. val map = mapOf("a" to 1, "b" to 2)
Solution
Step 1: Identify correct syntax for mutableMapOf
The correct syntax uses mutableMapOf with pairs using to keyword.
Step 2: Check each option
val map = mutableMapOf("a" to 1, "b" to 2) uses mutableMapOf and to correctly. val map = mapOf("a" -> 1, "b" -> 2) uses wrong arrow ->. val map = mutableMapOf("a" = 1, "b" = 2) uses = which is invalid here. val map = mapOf("a" to 1, "b" to 2) uses mapOf which creates an immutable map.
Final Answer:
val map = mutableMapOf("a" to 1, "b" to 2) -> Option A
Quick Check:
mutableMapOf + to = correct syntax [OK]
Hint: Use mutableMapOf with "key" to value pairs [OK]
Common Mistakes:
Using -> instead of to for pairs
Using = inside mapOf or mutableMapOf
Confusing mapOf and mutableMapOf
3. What is the output of this Kotlin code?
val map = mapOf("x" to 10, "y" to 20)
println(map["x"])
println(map["z"])
medium
A. 20
null
B. 10
null
C. null
null
D. 10
0
Solution
Step 1: Access existing key "x"
map["x"] returns 10 because "x" is a key with value 10.
Step 2: Access non-existing key "z"
map["z"] returns null because "z" is not in the map.
Hint: map[key] returns value or null if key missing [OK]
Common Mistakes:
Expecting 0 instead of null for missing key
Confusing values of different keys
Assuming map is mutable and can add keys
4. What is wrong with this Kotlin code?
val map = mapOf("a" to 1)
map["b"] = 2
medium
A. mapOf creates an immutable map, so you cannot add new entries
B. The syntax for adding entries is incorrect; should use put()
C. Keys must be integers, not strings
D. mapOf requires mutableMapOf to add entries
Solution
Step 1: Understand mapOf immutability
mapOf creates an immutable map that cannot be changed after creation.
Step 2: Adding entries to immutable map causes error
Trying to assign a new key-value pair with map["b"] = 2 causes a compile-time error because the map is immutable.
Final Answer:
mapOf creates an immutable map, so you cannot add new entries -> Option A
Quick Check:
Immutable map cannot be changed [OK]
Hint: Use mutableMapOf to add or change entries [OK]
Common Mistakes:
Trying to add entries to mapOf
Using wrong syntax for adding entries
Confusing mutableMapOf and mapOf
5. You want to create a map that stores student names as keys and their scores as values. You also want to update scores later. Which Kotlin code snippet correctly achieves this?
hard
A. val scores = mutableMapOf("Alice" to 90, "Bob" to 85)
scores.add("Alice", 95)
B. val scores = mapOf("Alice" to 90, "Bob" to 85)
scores["Alice"] = 95
C. val scores = mapOf("Alice" to 90, "Bob" to 85)
scores.put("Alice", 95)
D. val scores = mutableMapOf("Alice" to 90, "Bob" to 85)
scores["Alice"] = 95
Solution
Step 1: Choose a mutable map to allow updates
Since scores need to be updated, mutableMapOf is required.
Step 2: Use correct method to update value
Assigning with scores["Alice"] = 95 correctly updates the value in a mutable map.
Step 3: Check other options for errors
val scores = mapOf("Alice" to 90, "Bob" to 85)
scores["Alice"] = 95 uses immutable map and tries to update, causing error. val scores = mapOf("Alice" to 90, "Bob" to 85)
scores.put("Alice", 95) tries to use put on immutable map, error. val scores = mutableMapOf("Alice" to 90, "Bob" to 85)
scores.add("Alice", 95) uses non-existent add method.
Final Answer:
val scores = mutableMapOf("Alice" to 90, "Bob" to 85)
scores["Alice"] = 95 -> Option D
Quick Check:
mutableMapOf + assignment updates map [OK]
Hint: Use mutableMapOf and bracket assignment to update entries [OK]
Common Mistakes:
Using mapOf when updates are needed
Trying to use put on immutable map
Using add method which does not exist
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.