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
val sequence = listOf(1, 2, 3, 4, 5).asSequence()
val result = sequence
.filter { it % 2 == 0 }
.map { it * it }
.toList() // terminal operation triggers evaluationExplanation
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
fun main() {
val numbers = (1..1_000_000).asSequence()
val firstThreeSquaresOfEvens = numbers
.filter { it % 2 == 0 }
.map { it * it }
.take(3)
.toList()
println(firstThreeSquaresOfEvens)
}[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
1. What does asSequence() do to a Kotlin collection?
2. When are intermediate operations like filter and map on a Sequence actually executed?
3. Which of these is a terminal operation on a Sequence?
4. What is a key advantage of Sequences over eager collection operations for long chains?
5. For a small list with a single map operation, how does Sequence performance typically compare to a regular List operation?
Was this page helpful?
You May Also Like
Collection Operations in Kotlin (map, filter, fold)
How to transform, filter, and reduce Kotlin collections using functional-style operations like map, filter, and fold.
List, Set, and Map in Kotlin
A detailed look at Kotlin's three core collection types — List, Set, and Map — and how each stores and accesses data.
Collections in Kotlin
An overview of how Kotlin groups objects into Lists, Sets, and Maps using its dual mutable/read-only collection model.
Higher-Order Functions in Kotlin
Learn how Kotlin functions can accept or return other functions, enabling flexible, reusable, functional-style code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics