100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

List, Set, and Map in Kotlin

A detailed look at Kotlin's three core collection types — List, Set, and Map — and how each stores and accesses data.

CollectionsBeginner9 min readJul 8, 2026
Analogies

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

kotlin
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

kotlin
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"])
}
kotlin
[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

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#ListSetAndMapInKotlin#List#Set#Map#Syntax#DataStructures#StudyNotes#SkillVeris