Skip to main content

Map Basics in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

Map Basics is a fundamental concept every Kotlin developer should understand. Collections let you store and transform groups of values. Kotlin separates read-only and mutable views so you can express intent clearly in your APIs.

Map stores key-value pairs. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Map stores key-value pairs.
  • mapOf() creates an immutable map.
  • Keys must be unique; values can repeat.

Syntax

val map = mapOf("a" to 1, "b" to 2)

Map Basics in Kotlin Example Program in Kotlin

fun main(args: Array<String>) {
    val scores = mapOf("Alice" to 90, "Bob" to 85)
    println("Alice score: ${scores["Alice"]}")
    for ((name, score) in scores) {
        println("$name -> $score")
    }
}

Sample Output

Alice score: 90
Alice -> 90
Bob -> 85

When to use

Use collections when the number of items is dynamic or when you need map/set semantics instead of a plain list.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val scores = mapOf("Alice" to 90, "Bob" to 85) assigns or updates a value used later in the program.

  3. The println("Alice score: ${scores["Alice"]}") statement writes a line to the console — this produces part of the sample output below.

  4. The println("$name -> $score") statement writes a line to the console — this produces part of the sample output below.

  5. Map stores key-value pairs.

  6. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Prefer immutable listOf, setOf, mapOf for data that should not change after creation.
  • Use map, filter, and fold instead of manual loops when transforming collections.
  • Pick the smallest collection type that fits — don’t use a List when a Set is semantically correct.

Common Mistakes

  • Modifying a list while iterating it — use filter or iterator remove carefully.
  • Using mutableListOf when an immutable list would suffice, exposing accidental mutation.
  • Calling get on a Map without checking key existence — prefer getOrDefault or getValue.

Key Points

  • Map stores key-value pairs.
  • mapOf() creates an immutable map.
  • Keys must be unique; values can repeat.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Map Basics in Kotlin?
Map stores key-value pairs.
When should I use Map Basics?
Use collections when the number of items is dynamic or when you need map/set semantics instead of a plain list.
How is Map Basics different from Java?
Keys must be unique; values can repeat.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials