Kotlin Maps — Complete Guide

1. What is a Map in Kotlin?

A Map in Kotlin is a collection that stores data in key-value pairs. Each key is unique, while values can be duplicated. Maps are useful when you want to access data via a key rather than an index.

Example:

// Immutable Map
val capitals = mapOf("Pakistan" to "Islamabad", "USA" to "Washington")
println(capitals["Pakistan"]) // Islamabad

// Mutable Map
val mutableCapitals = mutableMapOf("India" to "New Delhi")
mutableCapitals["UK"] = "London"
println(mutableCapitals) // {India=New Delhi, UK=London}
            

2. How to Declare and Use Maps (with 4 Examples)

Example 1 — Using mapOf() (Immutable Map)

// Immutable Map
val userAges = mapOf("Ali" to 25, "Ahmed" to 30)
println(userAges["Ahmed"]) // 30
println(userAges.containsKey("Ali")) // true
        
Example 2 — Using mutableMapOf() (Mutable Map)

// Mutable Map
val scores = mutableMapOf("Math" to 90, "Science" to 85)
scores["English"] = 88   // Add new key-value pair
scores["Math"] = 95      // Update value
scores.remove("Science") // Remove entry

println(scores) // {Math=95, English=88}
        
Example 3 — Using HashMap and LinkedHashMap

// HashMap (unordered)
val hashMap = hashMapOf(1 to "A", 2 to "B", 3 to "C")
println(hashMap) // order not guaranteed

// LinkedHashMap (maintains insertion order)
val linkedMap = linkedMapOf(1 to "A", 2 to "B", 3 to "C")
println(linkedMap) // {1=A, 2=B, 3=C}
        
Example 4 — Traversing a Map

// Using for loop
for ((key, value) in scores) {
    println("$key -> $value")
}

// Using forEach
scores.forEach { (subject, score) ->
    println("$subject: $score")
}
        

3. Common Map Operations


val mapA = mapOf(1 to "One", 2 to "Two", 3 to "Three")
val mapB = mapOf(3 to "Three", 4 to "Four")

// Keys
println(mapA.keys) // [1, 2, 3]

// Values
println(mapA.values) // [One, Two, Three]

// Get value with default
println(mapA.getOrDefault(5, "Unknown")) // Unknown

// Merge Maps
val merged = mapA + mapB
println(merged) // {1=One, 2=Two, 3=Three, 4=Four}
        

4. When and When Not to Use Maps?

✅ When to Use Maps:
🚫 When Not to Use Maps:

5. Top 6 Interview Questions (With Answers)

  1. Q1: What is the main difference between Map and List?
    Ans: List stores elements by index and allows duplicates; Map stores elements as key-value pairs with unique keys.
  2. Q2: Can Map keys be duplicated?
    Ans: No, keys must be unique.
  3. Q3: How do you add or update entries in a mutable map?
    Ans: Use indexing or put() method:
    
    val map = mutableMapOf("A" to 1)
    map["B"] = 2
    map.put("A", 3)
                    
  4. Q4: How to traverse a Map?
    Ans: Use for ((key, value) in map) or forEach function.
  5. Q5: How to merge two maps?
    Ans: Use map1 + map2 for immutable merge.
  6. Q6: How to get a value with a default if key doesn’t exist?
    Ans: Use getOrDefault(key, defaultValue).
Bonus Tip 💡

In Android apps, Maps are ideal for storing settings, configuration, or key-value based data where keys need to be unique and access by key is fast.


val userSettings = mutableMapOf("theme" to "dark", "notifications" to true)
userSettings["theme"] = "light" // Update
println(userSettings) // {theme=light, notifications=true}