Learning Kotlin Programming
Kotlin Flow a much better version of Sequence?
Do we still need Sequence, after we have Flow?
Published in
12 min readJan 22, 2021
Sometime back, I’m curious how is List different from Sequence. I explored into it, and notice Sequence is Lazy and List is Slow. Each have their advantage.
With Kotlin Flow now in place, it behaves like a Sequence.
runBlocking {
(1..3).asSequence()
.map { println("sequence mapping $it"); it * 2 }
.first { it > 2 }
.let { println("sequence $it") }
(1..3).asFlow()
.map { println("flow mapping $it"); it * 2 }
.first { it > 2 }
.let { println("flow result $it") }
}
For the code above, both also behave like below
It will lazily process each item and terminate the process as soon as it get the result.