What a dictionary is
A dictionary stores key-value pairs. Instead of finding an item by its position, as in a list, you find it by its key. Each key appears once and maps to one value: a student's name to a score, a product code to a price, a word to its count.
Kotlin calls this structure a Map. The name "dictionary" is the general idea, and Map is what you write in code. A map's type has two parts, the key type and the value type. Map<String, Int> has text keys and whole-number values.
No import is needed. The map functions are available in every Kotlin file.
Read-only and mutable maps
As with lists, Kotlin separates maps you can read from maps you can change:
mapOf(...)creates a read-onlyMap.mutableMapOf(...)creates aMutableMap, which also lets you add, change and remove entries.
The lesson uses mutableMapOf, because the next lessons add and change entries.
Creating a map
There are two common ways to build one. The first starts empty, names the types, and fills the map with the index operator []:
val scores = mutableMapOf<String, Int>()
scores["Alice"] = 95
scores["Bob"] = 87
scores["Charlie"] = 72
Here the types go in angle brackets, because an empty map gives Kotlin nothing to infer them from. Assigning to a key that does not exist yet adds a new entry. The same syntax works for both reading and writing, and the later lessons show it in more detail.
The second way gives all the entries at once, as pairs:
val scores = mutableMapOf("Alice" to 95, "Bob" to 87, "Charlie" to 72)
This version needs no type arguments. Kotlin looks at the pairs and works out that the keys are String and the values are Int, so the map is a MutableMap<String, Int>.
How the "to" pairs work
The word to is not a keyword. It is a function written between its two operands, and it builds a Pair out of what is on its left and right. So "Alice" to 95 is a pair holding the key "Alice" and the value 95. The function mutableMapOf takes any number of pairs and turns each one into an entry.
The map prints in a readable form:
{Alice=95, Bob=87, Charlie=72}
Entries come out in the order you added them. That is how mutableMapOf behaves, and it is convenient when you print a map. Do not build logic on the order, though. Think of a map as a lookup table, not as a sequence.
The exercise
The task is to declare a map named scores holding three students' scores. The steps are short:
- Declare a
valnamedscores. Avalis enough, because the variable will always refer to the same map, even when its entries change. - Call
mutableMapOfand pass three pairs, one per student:"Alice"with95,"Bob"with87and"Charlie"with72.
val scores = mutableMapOf("Alice" to 95, "Bob" to 87, "Charlie" to 72)
The lesson's tests expect those exact names and numbers, so spell the keys with their capital letters, as they are written above.
Common mistakes
- Using
mapOf. It creates a read-only map. Reading works, but any later change, such asscores["Alice"] = 99, does not compile. - Using the wrong separator. Pairs are written with
to, not with a colon or=. Each pair is separated from the next by a comma. - Repeating a key. A key exists only once. If you write
"Alice" to 1and later"Alice" to 2in the same call, the later value replaces the earlier one, and the map keeps a single"Alice"entry. - Mixing value types. A
MutableMap<String, Int>holds only integers as values. A value like"ninety"is a type error. - Looking things up by position. A map has no indices. You ask for a key, not for the "second entry".
Key takeaways
- A map stores key-value pairs, and each key appears once.
mutableMapOf("a" to 1, "b" to 2)builds a map you can change;mapOfbuilds one you cannot.- The key and value types are inferred from the pairs, or given in angle brackets for an empty map.
- Entries are looked up by key with
map[key].