Introduction
List, Set, and Map are Kotlin's three fundamental collection interfaces, each designed for a different data shape. A List is an ordered collection that allows duplicate elements and supports indexed access. A Set is a collection of unique elements with no guaranteed positional index. A Map holds key-value pairs where each key maps to exactly one value. Choosing the right type for the job makes code both clearer and more efficient.
Cricket analogy: Like a batting order (List) where position matters and two players can share the same score, versus a list of unique player IDs (Set) with no ranking, versus a scorecard (Map) pairing each player's name to exactly one final score.
Syntax
val list: List<String> = listOf("a", "b", "a")
val set: Set<String> = setOf("a", "b", "a")
val map: Map<String, Int> = mapOf("a" to 1, "b" to 2)
val first = list[0]
val value = map["a"]
val valueOrThrow = map.getValue("a")Explanation
List preserves insertion order and permits duplicates, so list[0] always refers to the first element added (unless removed). Set discards duplicates; the default setOf() implementation is a LinkedHashSet, which preserves insertion order even though sets are conceptually unordered collections of unique items. Map associates each key with one value; accessing map["a"] returns a nullable value (null if the key is absent), while map.getValue("a") returns the value directly but throws a NoSuchElementException if the key is missing.
Cricket analogy: Like a batting order card that keeps the first-listed opener at slot one even if a later batsman is dropped, a List preserves insertion order; a Set of dismissed batsmen auto-removes duplicate name entries while still listing them in the order they got out.
Example
fun main() {
val list = listOf("apple", "banana", "apple")
val set = setOf("apple", "banana", "apple")
val map = mapOf("apple" to 3, "banana" to 5)
println(list)
println(set)
println(map["apple"])
println(map["cherry"])
}[apple, banana, apple]
[apple, banana]
3
null- List is ordered, allows duplicates, and supports indexed access with list[index].
- Set stores only unique elements; setOf() defaults to a LinkedHashSet that preserves insertion order.
- Map stores key-value pairs and is created with pairs like "key" to value.
- map[key] returns null when the key is missing; map.getValue(key) throws if missing.
- Choose List for ordered sequences, Set for uniqueness checks, and Map for lookups by key.
Practice what you learned
1. Which collection type allows duplicate elements?
2. What is the default implementation created by setOf()?
3. What does map["missingKey"] return if the key is not present?
4. How do you create a map entry pairing "x" with 10 in Kotlin?
5. Which method throws an exception if a key is missing from a Map?
Was this page helpful?
You May Also Like
Collections in Kotlin
An overview of how Kotlin groups objects into Lists, Sets, and Maps using its dual mutable/read-only collection model.
Collection Operations in Kotlin (map, filter, fold)
How to transform, filter, and reduce Kotlin collections using functional-style operations like map, filter, and fold.
Ranges in Kotlin
Learn how Kotlin ranges express sequences of values for iteration and containment checks using .., until, downTo, and step.
Sequences in Kotlin
How Kotlin Sequences provide lazy, element-by-element evaluation of collection operations for better performance on large chains.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics