Start learning free

LESSON 101 OF 348 · DICTIONARIES

Dictionaries in Kotlin: Creating a Map with mutableMapOf

Lesson Read the full explanation

In Java, a HashMap works like a dictionary — it stores key-value pairs where each key maps to a value. You need to import java.util.HashMap.

val scores = mutableMapOf<String, Int>()
scores["Alice"] = 95
scores["Bob"] = 87
scores["Charlie"] = 72

The type parameters <String, Integer> specify the key type and value type. Use put(key, value) to add entries:

val ages = mutableMapOf<String, Int>()
ages["Alice"] = 25
ages["Bob"] = 30
println(ages["Alice"])  // 25
println(ages.size)      // 2

Assignment
Follow the Coding Tutorial and create a HashMap named scores with three student scores!


Hint
Declare HashMap<String, Integer> scores = new HashMap<>() then use put() to add entries.

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · DICTIONARIES

    Dictionaries in Kotlin, explained

    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-only Map.
    • mutableMapOf(...) creates a MutableMap, 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:

    1. Declare a val named scores. A val is enough, because the variable will always refer to the same map, even when its entries change.
    2. Call mutableMapOf and pass three pairs, one per student: "Alice" with 95, "Bob" with 87 and "Charlie" with 72.
    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 as scores["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 1 and later "Alice" to 2 in 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; mapOf builds 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].
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free