Lesson 13 of 17

Maps

Maps in Kotlin

A Map stores key-value pairs. Create one with mapOf:

val scores = mapOf("Alice" to 95, "Bob" to 87)

Access values by key using square brackets:

println(scores["Alice"])  // 95
println(scores["Bob"])    // 87

Check the number of entries:

println(scores.size)  // 2

Check for a key:

println(scores.contains("Alice"))  // true

Your Turn

Create a map of three capitals: "France""Paris", "Japan""Tokyo", "Brazil""Brasília". Print the capital of France, the capital of Japan, and the map's size.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.