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
- listOf, setOf, mapOf: Read-Only Collections
- mutableListOf, mutableMapOf: Mutable Builders
- Accessing Elements: get, first, last, getOrNull
- Iterating and Transforming Collections with Standard Library