Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

maps-sets-kotlin

Maps and sets

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Maps and sets: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Slices and maps are reference types—capacity, nil safety, and iteration pitfalls appear in every code review.

You will apply Maps and sets in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Maps store key-value pairs; sets hold unique elements. Kotlin splits read-only Map/Set from MutableMap/MutableSet—similar discipline to lists and cleaner than raw JavaScript object maps.

Factories and access

val capitals = mapOf("FR" to "Paris", "DE" to "Berlin")
val tags = mutableSetOf("kotlin", "jvm")
println(capitals["FR"])

Use getValue when missing keys should throw; getOrDefault and getOrElse for safe fallbacks.

Important interview questions and answers

  1. Q: mapOf vs mutableMapOf?
    A: mapOf returns a read-only view; mutations require mutableMapOf or builders.
  2. Q: LinkedHashMap order?
    A: linkedMapOf preserves insertion order on JVM—useful for predictable iteration in tests.

Self-check

  1. How do you read a key that might be absent?
  2. What collection enforces uniqueness?

Pitfall: Reading missing map keys returns null—handle with ?: or getValue when key must exist.

Interview prep

Missing map key?

Returns null for map[key]—use ?: or getValue.

Set uniqueness?

Each element appears once.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • mapOf vs hashMap?
  • LinkedHashMap when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump