Flow operators help you change or pick data from a stream step by step. They make working with lists of data easy and clear.
Flow operators (map, filter, transform) in Kotlin
flowOf(1, 2, 3, 4, 5) .map { it * 2 } .filter { it > 5 } .transform { emit(it) emit(it + 1) }
map changes each item to something new.
filter keeps only items that match a condition.
transform lets you emit zero, one, or many items for each input.
flowOf(1, 2, 3) .map { it * 10 }
flowOf(1, 2, 3, 4) .filter { it % 2 == 0 }
flowOf(1, 2) .transform { emit(it) emit(it * 100) }
This program creates a flow of numbers 1 to 5. It multiplies each by 3, keeps only those greater than 7, then for each number it emits the number and the number plus one. Finally, it prints all emitted numbers.
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking fun main() = runBlocking { flowOf(1, 2, 3, 4, 5) .map { it * 3 } // multiply each by 3 .filter { it > 7 } // keep numbers greater than 7 .transform { emit(it) // emit the number emit(it + 1) // emit number plus one } .collect { println(it) } // print each emitted number }
Use map when you want exactly one output per input.
Use filter to remove unwanted items.
transform is more flexible and can emit many or no items per input.
map changes each item in the flow.
filter keeps only items that match a condition.
transform lets you emit any number of items for each input.
Practice
Solution
Step 1: Understand the purpose of
Themapmapoperator changes each item in the flow to a new value by applying a function.Step 2: Compare with other operators
filterkeeps items based on a condition,transformcan emit multiple items, andcollectgathers items but does not change them.Final Answer:
map-> Option BQuick Check:
Change each item =map[OK]
- Confusing
mapwithfilter - Thinking
transformonly changes one item - Using
collectto change items
numbers?Solution
Step 1: Identify the filter syntax
Thefilteroperator takes a lambda returning a Boolean to keep items matching the condition.Step 2: Check each option
numbers.filter { it % 2 == 0 } correctly usesfilter { it % 2 == 0 }to keep even numbers. numbers.map { it % 2 == 0 } usesmapwhich changes items to booleans. numbers.transform { emit(it % 2 == 0) } emits booleans instead of filtering. numbers.collect { it % 2 == 0 } misusescollect.Final Answer:
numbers.filter { it % 2 == 0 } -> Option CQuick Check:
Filter even numbers =filter { it % 2 == 0 }[OK]
- Using
mapinstead offilter - Misusing
collectas a filter - Confusing
transformsyntax
flowOf(1, 2, 3, 4)
.filter { it % 2 == 0 }
.map { it * 10 }
.toList()Solution
Step 1: Apply
The flow starts with [1, 2, 3, 4]. After filtering even numbers, we get [2, 4].filterto keep even numbersStep 2: Apply
Mapping [2, 4] by multiplying each by 10 results in [20, 40].mapto multiply each by 10Final Answer:
[20, 40] -> Option AQuick Check:
Filter evens then multiply by 10 = [20, 40] [OK]
- Mapping before filtering
- Confusing output with original list
- Ignoring order of operators
flowOf(1, 2, 3)
.filter { it > 1 }
.map { it * 2 }
.transform { emit(it + 1) }Solution
Step 1: Check the flow operators usage
The operatorsfilter,map, andtransformare used correctly in sequence.Step 2: Identify missing terminal operation
Flows are cold and need a terminal operator likecollectortoListto start processing. This code lacks that.Final Answer:
The code misses a terminal operator likecollect-> Option DQuick Check:
Flows need terminal operator to run [OK]
- Forgetting terminal operator
- Thinking
transformcan't followmap - Misreading filter condition syntax
transform to emit both the original number and its square for each item?Solution
Step 1: Understand
transformusagetransformallows emitting zero or more items per input, so emitting both original and square is possible.Step 2: Check each option's correctness
flow.transform { emit(it); emit(it * it) } correctly usestransformwith twoemitcalls. flow.map { listOf(it, it * it) } usesmapwhich emits a list, not separate items. flow.filter { emit(it); emit(it * it) } misusesfilter. flow.collect { emit(it); emit(it * it) } misusescollectwhich is terminal and cannot emit.Final Answer:
flow.transform { emit(it); emit(it * it) } -> Option AQuick Check:
Emit multiple items per input =transform[OK]
- Using map to emit multiple items separately
- Misusing filter or collect for emission
- Not calling emit inside transform
Ready to prove it?
Pass the test and this pattern moves unknown
