Sequences
In this page:
Creating a Sequence
Calling .asSequence() on a collection converts it into a Sequence, which processes elements lazily rather than eagerly building intermediate lists at each step.
Example: Creating a Sequence
fun main() {
val numbers = (1..5).asSequence()
println(numbers.toList())
}
Login to try C/C++/Java/PHP code in the editor
Lazy Evaluation with map and filter
Chaining .map { } and .filter { } on a sequence just builds up a pipeline of operations -- nothing actually runs until a terminal operation is called.
Example: Lazy Evaluation with map and filter
fun main() {
val result = listOf(1, 2, 3, 4, 5)
.asSequence()
.map { println("Mapping $it"); it * 2 }
.filter { it > 4 }
.toList()
println("Result: $result")
}
Login to try C/C++/Java/PHP code in the editor
Terminal Operations Trigger Execution
Functions like .toList(), .first(), .count(), or .sum() are terminal operations -- calling one is what actually runs the whole lazy pipeline and produces a concrete result.
Example: Terminal Operations Trigger Execution
fun main() {
val firstBigSquare = (1..100).asSequence()
.map { it * it }
.first { it > 50 }
println("First square over 50: $firstBigSquare")
}
Login to try C/C++/Java/PHP code in the editor
Sequences vs Regular Collections
Regular List operations like .map() eagerly build a full new list at each step, while a Sequence processes each element through the whole pipeline before moving to the next -- useful for long chains over large data.
Note: For small collections, plain list operations are usually simpler and fast enough; sequences shine with large data or many chained steps.
Example: Sequences vs Regular Collections
fun main() {
val eagerResult = (1..5).map { it * 2 }.filter { it > 4 }
val lazyResult = (1..5).asSequence().map { it * 2 }.filter { it > 4 }.toList()
println("Eager: $eagerResult")
println("Lazy: $lazyResult")
}
Login to try C/C++/Java/PHP code in the editor
- Using a regular
Listchain of.map().filter()on a huge dataset and being surprised by unnecessary intermediate lists; aSequenceavoids this by being lazy. - Forgetting to call
.toList()(or another terminal operation) at the end of a sequence chain, since intermediate operations are lazy and don't run until a terminal one is invoked. - Assuming sequences are always faster; for small collections, the overhead of building a sequence can outweigh the benefit compared to plain list operations.
asSequence()converts a collection into aSequence, which evaluates operations lazily, element by element.- Intermediate operations (
map,filter) on a sequence build up a pipeline but don't run until a terminal operation is called. - Terminal operations like
.toList(),.first(), or.sum()trigger the actual processing of a sequence. - Sequences avoid creating intermediate collections at each step, which can be more efficient for large data or long operation chains.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: