New to Akka? Start with the Akka SDK.

StreamConverters.javaCollector

Create a sink which materializes into a Future CompletionStage which will be completed with a result of the Java 8 Collector transformation and reduction operations.

Additional Sink and Source converters

Signature

StreamConverters.javaCollectorStreamConverters.javaCollector

Description

Creates a SinkSink that materializes into a FutureCompletionStage containing the result of applying a Java 8 java.util.stream.Collector to the incoming stream elements. The Collector will trigger demand downstream and will accumulate elements into a mutable result container, with an optional finisher transformation after all elements have been processed. Reduction processing is performed sequentially.

Note that a sink can be materialized multiple times, so the collectorFactory must create a fresh Collector for each materialization.

See also javaCollectorParallelUnordered for a parallel version, and Sink.collect for a convenience wrapper.

Example

In this example, we use StreamConverters.javaCollector with Collectors.toList to collect a stream of strings into a List.

Scala
sourceval source: Source[String, NotUsed] = Source(List("one", "two", "three"))

val sink: Sink[String, _] =
  StreamConverters.javaCollector(() => Collectors.toList[String]())
Java
sourceSource<String, NotUsed> source = Source.from(Arrays.asList("one", "two", "three"));

Sink<String, CompletionStage<List<String>>> sink =
    StreamConverters.javaCollector(Collectors::toList);

CompletionStage<List<String>> result = source.runWith(sink, system);
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.