Thuta Learning
IntermediateProgrammingbeginner

Collections (Maps)

Relax. We'll talk through this in plain words — no textbook voice.

A map is a collection that stores key-value pairs. It's used for paired data like username and age, product id and price, or setting name and setting value. Since you can look up a value using its key, lookups are fast and the code stays readable.

kotlin
fun main() {
    val ageMap = mapOf(
        "Alice" to 30,
        "Bob" to 25
    )

    println("Alice's age is ${ageMap["Alice"]}")
}
You should see
Alice's age is 30

Summary

A map is a clean, practical collection for key-value data.

Easy traps

  • If you misspell the key, you might get null back instead of the value. For example, "alice" and "Alice" are not the same.

Practical example — price lookup

Practical example — price lookup

kotlin
fun main() {
    val priceMap = mapOf("coffee" to 2500, "tea" to 1500)
    val item = "coffee"
    val price = priceMap[item] ?: 0

    println("$item price: $price MMK")
}

You'll know it worked when: coffee price: 2500 MMK