zip

Combines elements from each of multiple sources into tuples Pair and passes the tuples pairs downstream.

Fan-in operators

Signature

def zip[U](that: Graph[SourceShape[U], _]): Repr[(Out, U)]
def zipMat[U, Mat2, Mat3](that: Graph[SourceShape[U], Mat2])(matF: (Mat, Mat2) ⇒ Mat3): ReprMat[(Out, U), Mat3]

Description

Combines elements from each of multiple sources into tuples Pair and passes the tuples pairs downstream.

emits when all of the inputs have an element available

backpressures when downstream backpressures

completes when any upstream completes

Example

Scala
import akka.stream.scaladsl.Source
import akka.stream.scaladsl.Sink

val sourceFruits = Source(List("apple", "orange", "banana"))
val sourceFirstLetters = Source(List("A", "O", "B"))
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(println))
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
Java
import akka.stream.javadsl.Source;
import akka.stream.javadsl.Sink;
import java.util.Arrays;

Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
Source<String, NotUsed> sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B"));
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), materializer);
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
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.