Kotlin 2 · Collections and lambdas

Process lazily

Sequences compute items on demand, which avoids intermediate lists for large pipelines.

val firstSquares = generateSequence(1) { it + 1 }
    .map { it * it }
    .filter { it % 2 == 1 }
    .take(3)
    .toList()

Official Kotlin 2 documentation

More collections and lambdas commands

All collections and lambdas commands · Kotlin 2 cheatsheet

Was this page helpful?