mapConcat

Transform each element into zero or more elements that are individually passed downstream.

Simple operators

Signature

Source.mapConcatSource.mapConcat Flow.mapConcatFlow.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.

Scala
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
Java
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

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.