Kotlin Tutorial

Collections

Lists, Sets, Maps, and Immutability in Kotlin

Read-Only vs. Mutable

Wait! Unlike Java, Kotlin makes a clear distinction between Read-Only (Immutable) and Mutable collections. This is a key safety feature.
  1. Read-Only: You can read data but not add or remove items. (e.g., listOf(), setOf(), mapOf()).
  2. Mutable: You can add, remove, and update items. (e.g., mutableListOf(), mutableSetOf()).
kotlin
val list = listOf("A", "B") // Cannot add items
val mutableList = mutableListOf("A", "B")
mutableList.add("C") // SUCCESS!

Creating Collections

CollectionRead-OnlyMutable
ListlistOf(1, 2)mutableListOf(1, 2)
SetsetOf(1, 1, 2)mutableSetOf(1, 2)
MapmapOf("A" to 1)mutableMapOf("A" to 1)

Collection Transformations

Kotlin provides a rich set of functional operations to transform your data.
kotlin
val numbers = listOf(1, 2, 3, 4, 5)

val evens = numbers.filter { it % 2 == 0 } // [2, 4]
val doubled = numbers.map { it * 2 }       // [2, 4, 6, 8, 10]
val first = numbers.first()                // 1

The it Keyword

In Kotlin, if a lambda has only one parameter, you can refer to it using the shorthand it.
kotlin
list.forEach { println(it) }

Summary

TermDescription
ListOrdered collection of items
SetUnordered collection of unique items
MapKey-Value pairs
MutableCollection that can be changed
filterKeep items that match a condition
mapTransform every item in a list
Best ForOrganizing and processing data streams
LogicFavor read-only collections whenever possible
SafetyPrevents accidental modifications in concurrent code
ModernFunctional programming style for data manipulation
StandardUse List<T> as the default collection type
IdentityCollections are backed by standard Java collections for performance