Map Container in Kotlin

Level 3107 keystrokes

Creates a map and reads values by key, using a default value for a missing key.

Kotlin code

// mapOf with key to value pairs creates a read-only map
1val ages = mapOf("Alice" to 25, "Bob" to 30)
2
// [key] returns the value, or null if the key is missing: prints 25
3println(ages["Alice"])
// "Carol" is not a key, so getOrDefault returns the default 0
4println(ages.getOrDefault("Carol", 0))

Output

25
0
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • "× 8
  • (× 4
  • )× 4
  • ,× 2
  • .× 1
  • [× 1
  • ]× 1
  • =× 1

More Kotlin examples at level 3

← Kotlin typing practice