Kotlin
collect
Learn to consume Flows with collect.
collect: Terminal operator
collect is a terminal operator: it starts the flow and consumes its values. Unlike intermediate operators (map, filter), without collect the flow does nothing. This is why a Flow is called cold.
fun numbers(): Flow<Int> = flowOf(1, 2, 3)
fun main() = runBlocking {
numbers().collect { println(it)
}
}
collect is a suspend function; it can only be called inside a coroutine. The lambda runs once for each emitted value, in order. When the flow ends, collect returns and main continues.
collect inside a coroutine
Because collect is a suspend function, it can only be called from inside a coroutine. Using runBlocking { ... } in main bridges the flow to a synchronous call. The program then waits until the flow finishes.
fun main() = runBlocking {
var sum = 0
numbers().collect { sum += it }
println(sum)
}
Here collect runs sum += it for each value; when the flow ends, println(sum) runs. Mutating an outer variable (here sum) from the lambda is safe for small flows. For more complex cases prefer operators like fold.
Exercise Collect values into a sum
Define fun numbers(): Flow<Int> = flowOf(1, 2, 3). Mark main with runBlocking. Inside it initialise var sum = 0; collect with numbers().collect { sum += it }; print the result with println(sum).
Show example solution
fun numbers(): Flow<Int> = flowOf(1, 2, 3)
fun main() = runBlocking {
var sum = 0
numbers().collect { sum += it }
println(sum)
}
Test yourself
-
What does flow.collect { ... } do?
- Consumes the flow; runs the lambda for each value
- Converts the flow to a list
- Starts the flow but receives no values
- Cancels the flow
Show answer
Consumes the flow; runs the lambda for each value — collect is terminal; it runs the lambda for each emitted value.
-
Match the collect concepts
- collect { }
- Consumer
- terminal
- Final operation
- suspend
- Pausing
- println(it)
- Prints the value
-
Consume the flow
fun main() = runBlocking {
numbers().____ { println(it) }
}
Show answer
fun main() = runBlocking {
numbers().collect { println(it) }
}
Next lesson: map and filter