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
- Q: mapOf vs mutableMapOf?
A:mapOfreturns a read-only view; mutations requiremutableMapOfor builders. - Q: LinkedHashMap order?
A:linkedMapOfpreserves insertion order on JVM—useful for predictable iteration in tests.
Self-check
- How do you read a key that might be absent?
- 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.