listOf, setOf, mapOf: Read-Only Collections
Build immutable collections and understand their access patterns.
listOf, setOf, mapOf: Read-Only Collections is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Three Builder Functions
Kotlin's stdlib offers listOf, setOf, and mapOf to construct read-only collections. Read-only means you cannot add or remove through the interface.
Building a List
listOf creates a read-only List<T>. Element type is inferred from arguments.
fun main() {
val fruits = listOf("apple", "banana", "cherry")
println(fruits) // [apple, banana, cherry]
println(fruits.size) // 3
}Building a Set
setOf returns a read-only Set<T>. Duplicate elements are collapsed automatically.
fun main() {
val tags = setOf("kotlin", "java", "kotlin", "android")
println(tags) // [kotlin, java, android]
println(tags.size) // 3
}Building a Map
mapOf takes key to value pairs and returns a read-only Map<K, V>.
fun main() {
val ages = mapOf("Alice" to 30, "Bob" to 25)
println(ages["Alice"]) // 30
println(ages.keys) // [Alice, Bob]
}Type Annotations
If the inferred type is wrong (or the list is empty), provide explicit type parameters.
fun main() {
val empty: List<String> = listOf()
val numbers: List<Int> = listOf(1, 2, 3)
val emptyMap: Map<String, Int> = mapOf()
println("empty=${empty}, n=${numbers}, m=${emptyMap}")
}Empty Variants
Use emptyList(), emptySet(), emptyMap() when you explicitly want an empty collection. Better than listOf() with a type parameter when intent matters.
fun main() {
val xs = emptyList<String>()
val ys = emptySet<Int>()
val zs = emptyMap<String, Int>()
println("${xs.size}, ${ys.size}, ${zs.size}") // 0, 0, 0
}Read-Only Means... Read-Only
The List interface has no add. The compiler refuses to mutate.
fun main() {
val items = listOf(1, 2, 3)
// items.add(4) // ERROR: List has no add
val bigger = items + 4 // creates new list
println(bigger) // [1, 2, 3, 4]
}Underlying Implementation
Under the hood, listOf returns a Java ArrayList wrapped in an unmodifiable view. Read-only is an interface contract, not a runtime guarantee against reflection.
fun main() {
val xs = listOf(1, 2, 3)
println(xs::class.simpleName) // ArrayList
}Pairs and to
The infix function to creates a Pair<A, B> — handy for map literals and tuples.
fun main() {
val pair = "key" to 42
println(pair) // (key, 42)
val m = mapOf("a" to 1, "b" to 2)
for ((k, v) in m) println("$k -> $v")
}Iterating Read-Only Collections
Read-only collections support all the standard iteration patterns: for, forEach, map, filter.
fun main() {
val nums = setOf(1, 2, 3, 4)
val doubled = nums.map { it * 2 }
println(doubled) // [2, 4, 6, 8]
}Concatenating Lists
The + operator produces a new combined list (read-only behavior preserved).
fun main() {
val a = listOf(1, 2)
val b = listOf(3, 4)
val combined = a + b
println(combined) // [1, 2, 3, 4]
}Quick Check
What happens when you call add(4) on a list created with listOf(1, 2, 3)?
Recap
listOf, setOf, and mapOf create read-only collections. They cannot be modified through their interfaces. Use + to produce new collections, and emptyList()/emptySet()/emptyMap() for typed empties.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “listOf, setOf, mapOf: Read-Only Collections” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “listOf, setOf, mapOf: Read-Only Collections”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “listOf, setOf, mapOf: Read-Only Collections”?
Build immutable collections and understand their access patterns. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “listOf, setOf, mapOf: Read-Only Collections” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- listOf, setOf, mapOf: Read-Only Collections
- mutableListOf, mutableMapOf: Mutable Builders
- Accessing Elements: get, first, last, getOrNull
- Iterating and Transforming Collections with Standard Library