0Pricing
Kotlin Academy · Lesson

Lazy Operations

Avoid intermediate lists.

Lazy Operations

The power of sequences comes from lazy operations. Intermediate operations build a pipeline without running it, and only a terminal operation pulls elements through. This avoids intermediate lists.

No Intermediate Lists

With collections, each map and filter allocates a new list. With sequences, elements stream through all steps without creating those temporary lists.

fun main() {
    val result = (1..6).asSequence()
        .map { it * it }
        .filter { it % 2 == 0 }
        .toList()
    println(result)
}

All lessons in this course

  1. Why Sequences
  2. Creating Sequences
  3. Lazy Operations
  4. Sequences vs Collections
← Back to Kotlin Academy