Flow operators.
A raw flow emits values. But you often need to transform, filter, or combine those values. Flow operators let you build pipelines that process data elegantly. Today we explore the most important flow operators.
Flow operators come in two kinds.
Intermediate operators transform a flow into another flow. They are lazy. They do not start flow execution.
Terminal operators start the flow and produce a result. They trigger execution. collect is a terminal operator.
You chain intermediate operators to build a pipeline. Then a terminal operator runs it.
flow dot map transform dot filter condition dot collect process. map and filter are intermediate. collect is terminal.
map transforms each emitted value.
flow dot map brace it times 2 close brace. Each value passes through the transformation.
map takes a lambda that receives the emitted value and returns a transformed value.
The result is a new flow. The original flow is unchanged. Operators create new flows, they do not modify existing ones.
Use map when you need to convert each value. String to Int. User to UserDTO. Raw data to display format.
filter keeps only values that match a condition.
flow dot filter brace it greater than 0 close brace. Only positive values pass through.
filter takes a predicate. True means keep the value. False means drop it.
The result is a flow with fewer values. Some values are filtered out.
Use filter to remove unwanted values. Errors, nulls, items that do not match criteria.
transform gives you full control.
Inside transform, you call emit yourself. You can emit zero, one, or multiple values for each input.
flow dot transform brace value arrow emit value emit value times 2 close brace. Each input produces two outputs.
transform is more powerful than map. map emits exactly one value per input. transform can emit any number.
Use transform when you need to expand, flatten, or conditionally emit values.
take limits the number of values.
flow dot take 3. Only the first three values pass. After that, the flow cancels.
drop skips values.
flow dot drop 2. The first two values are ignored. The rest pass through.
takeLast and dropLast work from the end. But they must buffer values, so they wait for completion.
Use take for pagination or limiting. Use drop to skip headers or initial values.
distinctUntilChanged removes consecutive duplicates.
flow emits 1 comma 1 comma 2 comma 2 comma 1. After distinctUntilChanged, you get 1 comma 2 comma 1.
Only consecutive duplicates are removed. The final 1 is not a duplicate of anything immediately before it.
Use this for event streams. User moves mouse, many events with same position. You only want when position changes.
combine merges two flows.
flow1 dot combine flow2 brace a comma b arrow a plus b close brace. When either flow emits, the lambda runs with the latest values from both.
combine is useful for combining independent data sources. User name flow plus user status flow gives combined user info.
The lambda runs whenever either flow emits, using the most recent value from each.
Both flows must emit at least once before the first combined value appears.
zip pairs values from two flows.
flow1 dot zip flow2 brace a comma b arrow Pair a comma b close brace. First from flow1 pairs with first from flow2.
Unlike combine, zip waits for matching values. If one flow is faster, it waits for the other.
The result has as many values as the shorter flow. Extra values from the longer flow are ignored.
Use zip when values have a natural pairing. Request-response. Question-answer.
flatMapConcat transforms each value into a flow, then concatenates the results.
flow dot flatMapConcat brace value arrow anotherFlow value close brace. Each value becomes a flow. Those flows are collected in order.
Concat means sequential. First inner flow completes, then next inner flow starts.
Use flatMapConcat when order matters. Process each item's subflow completely before moving to the next.
Terminal operators consume the flow and produce a result.
collect receives each value. The most common terminal operator.
toList collects all values into a list. Waits for completion.
toSet collects into a set. Removes duplicates.
first returns the first value. Cancels the flow after.
single returns the only value. Throws if zero or more than one.
reduce combines values. reduce brace acc comma value arrow acc plus value close brace.
Let us summarize.
Intermediate operators transform flows: map, filter, transform, take, drop, distinctUntilChanged.
Combination operators merge flows: combine, zip, flatMapConcat.
Terminal operators produce results: collect, toList, first, reduce.
Operators are lazy until a terminal operator runs the pipeline.
Chain operators to build expressive data processing pipelines.