What this thread does
Do first: place asSequence() before filter and map, then use take(n).toList() only if the caller needs the first n matching results.
Short answer
As of November 2026, Kotlin 2.4.21 supports lazy sequence pipelines, but converting a collection is a performance choice, not an automatic speed improvement. Avoid intermediate lists when useful work can stop early; compare elapsed time and allocations for the real input and result size (sequence guide).
Choose / don't choose
- Selective early result: choose a sequence when only a bounded number of matches is needed from a large input; don't materialize the entire result before taking the prefix (take, terminal collection).
- Small/simple full result: keep eager collection operations when lazy dispatch costs more than the saved allocations. Chris Banes' 13 February 2025 measurements found lists faster in his list/sequence comparisons, including a larger input; those measurements do not determine your pipeline's crossover point.
- Required side effects: use an explicitly consumed operation; actual computing happens when the whole chain result is requested (evaluation).
Not for: coroutine cancellation or asynchronous Flow processing.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
asSequence() | Convert the original collection before any intermediate eager filter/map list is built | Kotlin API version selector: 2.4 | collection adapter |
filter | Put the predicate before expensive mapping when selection depends on the original element; intermediate and stateless | Kotlin API version selector: 2.4 | filter |
map | Transform only the surviving elements; intermediate and stateless | Kotlin API version selector: 2.4 | map |
take(n) | Place after filtering to count matching results; n must be nonnegative; still intermediate | Kotlin API version selector: 2.4 | take |
toList() | Terminal operation; returns a list containing all elements | Kotlin API version selector: 2.4 | toList |
Order of operations
1. Keep the original predicate/mapping semantics, then change only the evaluation strategy. A schematic pipeline is items.asSequence().filter { predicate(it) }.map { transform(it) }.take(n).toList(); predicate/transform are application callbacks. 2. For a sequence, map executes on an element that passes filtering before the next element is filtered. Eager filter first visits the whole collection, eager map then transforms the filtered collection, and take follows filter and map in the example (side-by-side order). 3. Compare output equality and callback counts before comparing cost. If the predicate depends on mapped data, retain the required mapping-before-filter order rather than mechanically rearranging operations. This is a semantics-preservation recommendation based on the documented per-element execution. 4. Consume with toList() once a list is required (toList, adapter).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| Callbacks never execute (symptom) | Sequence elements require a terminal operation to be retrieved | Add the required terminal consumption; toList() produces a list | lazy/terminal distinction, terminal API |
IllegalArgumentException | take(n) receives negative n | Reject an invalid limit before building the pipeline | argument contract |
| Sequence is slower (symptom) | Banes' list/sequence comparisons favored lists | Retain eager operations when comparable measurements favor them | Banes' measurements |
Missing
- There is no universal input size or chain length where sequences become faster. Cost depends on callbacks, early termination, runtime and result materialization.
Checked
2026-11-12; Kotlin 2.4.21 release history, sequence guide and linked 2.4 APIs plus Banes' dated measurements read. No Kotlin benchmark commands executed.
Started from a post by @JetBrains, packaged by the editor. Original post