Source.zipN
Combine the elements of multiple sources into a source of sequences of value.
Signature
Description
Collects one element for every upstream and when all upstreams has emitted one element all of them are emitted downstream as a collection. The element order in the downstream collection will be the same order as the sources were listed.
Since the sources are provided as a list the individual types are lost and the downstream sequences will end up containing the closest supertype shared by all sourcesyou may have to make sure to have sources type casted to the same common supertype of all stream elements to use zipN
.
See also:
Example
In this sample we zip a stream of characters, a stream of numbers and a stream of colours. Into a single Source
where each element is a Vector
List
of [character, number, color]
:
- Scala
-
source
val chars = Source("a" :: "b" :: "c" :: "e" :: "f" :: Nil) val numbers = Source(1 :: 2 :: 3 :: 4 :: 5 :: 6 :: Nil) val colors = Source("red" :: "green" :: "blue" :: "yellow" :: "purple" :: Nil) Source.zipN(chars :: numbers :: colors :: Nil).runForeach(println) // prints: // Vector(a, 1, red) // Vector(b, 2, green) // Vector(c, 3, blue) // Vector(e, 4, yellow) // Vector(f, 5, purple)
- Java
-
source
Source<Object, NotUsed> chars = Source.from(Arrays.asList("a", "b", "c", "e", "f")); Source<Object, NotUsed> numbers = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6)); Source<Object, NotUsed> colors = Source.from(Arrays.asList("red", "green", "blue", "yellow", "purple")); Source.zipN(Arrays.asList(chars, numbers, colors)).runForeach(System.out::println, system); // prints: // [a, 1, red] // [b, 2, green] // [c, 3, blue] // [e, 4, yellow] // [f, 5, purple]
Note how it stops as soon as any of the original sources reaches its end.
Reactive Streams semantics
emits when all of the inputs has an element available
completes when any upstream completes
backpressures all upstreams when downstream backpressures but also on an upstream that has emitted an element until all other upstreams has emitted elements