Collection Operations in Kotlin

From the Kotlin cheat sheet · Collections · verified Jul 2026

Collection Operations

Transformations and aggregations

kotlin
// Transformations
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
val filtered = numbers.filter { it > 2 }
val strings = numbers.map { "Item $it" }

// Aggregations
val sum = numbers.sum()
val average = numbers.average()
val max = numbers.maxOrNull()
val count = numbers.count { it % 2 == 0 }

// Grouping
val words = listOf("apple", "apricot", "banana", "blueberry")
val grouped = words.groupBy { it.first() }
// {a=[apple, apricot], b=[banana, blueberry]}

// Flattening
val nested = listOf(listOf(1, 2), listOf(3, 4))
val flat = nested.flatten()  // [1, 2, 3, 4]
val flatMapped = nested.flatMap { it.map { n -> n * 2 } }  // [2, 4, 6, 8]
💡 Collection operations return new collections (immutable)
⚡ Use sequences for lazy evaluation of large collections
📌 fold provides initial value, reduce uses first element
🟢 partition splits collection into two based on predicate

More Kotlin tasks

Back to the full Kotlin cheat sheet