0Pricing
Kotlin Academy · Lesson

listOf, setOf, mapOf: Read-Only Collections

Build immutable collections and understand their access patterns.

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
}

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