Source.runForeach vs Sink based approach

I have 2 approaches as below. Second one uses Sink. Both give same output.

Source<Integer, NotUsed> intSource = Source.range(1 ,7);
intSource.runForeach((elem) -> {System.out.println(elem);}, mat);
Sink<Integer, CompletionStage> intSink = Sink.foreach((elem) -> {System.out.println(elem);});
RunnableGraph graph =;;

Which one to use when?What’s the conceptual difference between two?

Both are the same. The first is more convenient to use in this case, and it gives the CompletionStage materialized value as return value.

Note that you can also use .runWith(sink) instead of .to(sink).run(). The difference there is also the materialized value of the sink vs source (right vs left).

1 Like