Flow Operators: map, filter, transform, and take
Apply intermediate operators to transform and limit Flow emissions.
What Is a Flow?
A Flow is a cold, asynchronous stream. Intermediate operators like map, filter, and transform are lazy — they run only when collected.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun numbers(): Flow<Int> = flow {
repeat(5) { emit(it + 1) }
}
fun main() = runBlocking {
numbers().collect { println(it) }
}map Operator
map transforms each emitted value. It is a 1-to-1 transformation applied lazily.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
(1..5).asFlow()
.map { it * it }
.collect { println(it) } // 1 4 9 16 25
}All lessons in this course
- Flow Operators: map, filter, transform, and take
- catch and onCompletion: Error Handling in Flow
- combine and zip: Merging Multiple Flows
- flowOn and buffer: Context and Backpressure