mapConcat
Transform each element into zero or more elements that are individually passed downstream.
Signature¶
Source.mapConcat
Flow.mapConcat
Description¶
Transform each element into zero or more elements that are individually passed downstream. This can be used to flatten collections into individual stream elements. Returning an empty iterable results in zero elements being passed downstream rather than the stream being cancelled.
See also statefulMapConcat, flatMapConcat, flatMapMerge
Example¶
The following takes a stream of integers and emits each element twice downstream.
sourcedef duplicate(i: Int): List[Int] = List(i, i)
Source(1 to 3).mapConcat(i => duplicate(i)).runForeach(println)
// prints:
// 1
// 1
// 2
// 2
// 3
// 3
sourceIterable<Integer> duplicate(int i) {
return Arrays.asList(i, i);
}
Source.from(Arrays.asList(1, 2, 3))
.mapConcat(i -> duplicate(i))
.runForeach(System.out::println, system);
// prints:
// 1
// 1
// 2
// 2
// 3
// 3
Reactive Streams semantics¶
emits when the mapping function returns an element or there are still remaining elements from the previously calculated collection
backpressures when downstream backpressures or there are still available elements from the previously calculated collection
completes when upstream completes and all remaining elements has been emitted