Junior — Middle
Can you list the operators used in Flow?
sobes.tech AI
Answer from AI
In Kotlin Flow, the following main operators are used for working with data streams:
- transform — transforms stream elements.
- map — applies a function to each element and returns a new stream.
- filter — passes only elements satisfying a condition.
- take — takes the first N elements.
- collect — collects elements from the stream (terminal operator).
- flatMapConcat, flatMapMerge, flatMapLatest — for transforming and combining streams.
- onEach — performs an action for each element without changing the stream.
- buffer — adds buffering for performance.
- debounce — filters out elements that arrive too frequently.
Example of using map and filter:
flowOf(1, 2, 3, 4, 5)
.filter { it % 2 == 0 }
.map { it * it }
.collect { println(it) } // Outputs 4 and 16
These operators allow flexible processing of asynchronous data streams.