0Pricing
Kotlin Academy · Lesson

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 — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “listOf, setOf, mapOf: Read-Only Collections” lesson free?

Yes — the full text of “listOf, setOf, mapOf: Read-Only Collections” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. listOf, setOf, mapOf: Read-Only Collections
  2. mutableListOf, mutableMapOf: Mutable Builders
  3. Accessing Elements: get, first, last, getOrNull
  4. Iterating and Transforming Collections with Standard Library
← Back to Kotlin Academy