Collections (List, Set, Map) (Kotlin)
Kotlin's standard collections are List (ordered), Set (unique), and Map (key–value). Each comes in a read-only interface and a Mutable variant, making immutability the default.
Kotlin
listOf, setOf, and mapOf create read-only collections; mutableListOf, mutableSetOf, and mutableMapOf create modifiable ones. The read-only interfaces (List, Set, Map) don't expose add/remove, so a function taking a List can't mutate your data — favoring this is idiomatic.
All collections share a rich set of extension operations — map, filter, forEach, groupBy, associate, sortedBy, fold — so you rarely write manual loops. These return new collections rather than mutating in place.
Read-only is not the same as immutable: a List reference can still point to a list that something else mutates through a MutableList reference. For guaranteed immutability use immutable collection libraries or defensive copies.
Syntax
val nums = listOf(1, 2, 3)
val ages = mapOf("ana" to 30, "sam" to 25)Examples
Kotlin
val fruits = mutableListOf("apple", "pear")
fruits.add("plum")
val ages = mapOf("ana" to 30)
println(fruits)
println(ages["ana"])Output:
[apple, pear, plum]
30