Introduction
A collection is a group of a variable number of items (possibly zero) that share significance and are processed together. Kotlin's standard library provides a rich set of tools for creating, populating, and managing collections of any type. Unlike Java, Kotlin makes a clear distinction at the interface level between read-only and mutable collections, which helps prevent accidental modification of data and encourages a more functional programming style.
Cricket analogy: A cricket squad list is a collection: a variable number of players sharing the purpose of representing the team, and Kotlin's read-only vs mutable split is like a public team-sheet you can view versus the selector's editable roster.
Syntax
// Read-only collections
val readOnlyList: List<Int> = listOf(1, 2, 3)
val readOnlySet: Set<String> = setOf("a", "b")
val readOnlyMap: Map<String, Int> = mapOf("x" to 1)
// Mutable collections
val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)
val mutableSet: MutableSet<String> = mutableSetOf("a", "b")
val mutableMap: MutableMap<String, Int> = mutableMapOf("x" to 1)Explanation
Kotlin splits its collection interfaces into read-only (List, Set, Map) and mutable (MutableList, MutableSet, MutableMap) versions. The read-only interfaces expose only operations that access elements, while the mutable interfaces add operations like add(), remove(), and put(). It is important to understand that a read-only List is not necessarily immutable: it is simply a view that lacks mutating methods. If the same underlying collection is also referenced through a MutableList, changes made through that reference will be visible through the read-only view as well.
Cricket analogy: List/Set/Map for viewing versus MutableList/MutableSet/MutableMap for editing is like a fan's read-only scoreboard app versus the official scorer's editable console; if both point to the same live match data, a run the scorer adds instantly shows on the fan's view too.
Example
fun main() {
val numbers = mutableListOf(1, 2, 3)
val readOnlyView: List<Int> = numbers // same underlying list
numbers.add(4)
println(readOnlyView) // reflects the change made via numbers
val fruits = listOf("apple", "banana", "cherry")
println("Total fruits: ${fruits.size}")
}[1, 2, 3, 4]
Total fruits: 3- Kotlin collections come in read-only (List, Set, Map) and mutable (MutableList, MutableSet, MutableMap) flavors.
- Read-only means 'no mutating methods on this interface', not guaranteed deep immutability of the underlying data.
- listOf(), setOf(), and mapOf() create read-only collections; mutableListOf(), mutableSetOf(), mutableMapOf() create mutable ones.
- Choosing read-only interfaces by default improves code safety and predictability.
- The three core collection types are List (ordered), Set (unique elements), and Map (key-value pairs).
Practice what you learned
1. Which function creates a read-only List in Kotlin?
2. What does 'read-only' mean for a Kotlin List?
3. Which interface adds add() and remove() methods to a List?
4. Which of these is NOT one of Kotlin's three core collection types?
5. What happens if you hold both a List and a MutableList reference to the same underlying collection and mutate via the MutableList reference?
Was this page helpful?
You May Also Like
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.
Collection Operations in Kotlin (map, filter, fold)
How to transform, filter, and reduce Kotlin collections using functional-style operations like map, filter, and fold.
Sequences in Kotlin
How Kotlin Sequences provide lazy, element-by-element evaluation of collection operations for better performance on large chains.
Loops in Kotlin (for, while, do-while)
Understand Kotlin's for, while, and do-while loops, along with break, continue, and labeled loops.
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