100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Sequences in Kotlin

How Kotlin Sequences provide lazy, element-by-element evaluation of collection operations for better performance on large chains.

CollectionsIntermediate9 min readJul 8, 2026
Analogies

Introduction

A Sequence in Kotlin represents a lazily-evaluated collection of elements. Unlike List or Set, whose operations like map and filter are eager and produce a new collection at every step, Sequence operations are evaluated lazily: intermediate operations are only recorded, and actual computation happens element-by-element only when a terminal operation is invoked. This can significantly reduce the number of intermediate allocations, especially for long operation chains over large data sets.

🏏

Cricket analogy: Filtering every ball bowled this season eagerly builds a full new list at each step (like List operations), whereas a Sequence processes ball-by-ball lazily, only computing results when you actually ask for the final tally, saving memory on huge datasets.

Syntax

kotlin
val sequence = listOf(1, 2, 3, 4, 5).asSequence()

val result = sequence
    .filter { it % 2 == 0 }
    .map { it * it }
    .toList() // terminal operation triggers evaluation

Explanation

asSequence() converts an existing collection into a Sequence without eagerly copying or transforming it. Intermediate operations such as filter and map on a Sequence are lazy: calling them just builds up a pipeline of operations to apply, without touching any elements yet. Only when a terminal operation like toList(), first(), sum(), or forEach() is called does the sequence process elements one at a time, applying all intermediate operations to each element before moving to the next. This avoids building full intermediate lists at each step, which is more efficient for long chains or very large collections, though for small collections or single operations the overhead of sequence bookkeeping can make eager collection operations just as fast or faster.

🏏

Cricket analogy: Converting a season's ball-by-ball list with asSequence() doesn't copy it; filter and map just queue up 'find sixes' and 'convert to runs' as a pipeline, and only when you call sum() does it actually walk through and tally the runs one ball at a time.

Example

kotlin
fun main() {
    val numbers = (1..1_000_000).asSequence()

    val firstThreeSquaresOfEvens = numbers
        .filter { it % 2 == 0 }
        .map { it * it }
        .take(3)
        .toList()

    println(firstThreeSquaresOfEvens)
}
kotlin
[4, 16, 36]
  • asSequence() converts a collection into a lazily-evaluated Sequence.
  • Intermediate operations like filter and map on a sequence are only recorded, not executed immediately.
  • A terminal operation such as toList(), first(), or sum() triggers the actual element-by-element evaluation.
  • Sequences avoid creating full intermediate collections at each step, which helps with large data or long chains.
  • For small collections or a single operation, eager List operations may be simpler and just as efficient.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#SequencesInKotlin#Sequences#Syntax#Explanation#Example#StudyNotes#SkillVeris