StreamConverters.javaCollector
Create a sink which materializes into a Future CompletionStage which will be completed with a result of the Java Collector transformation and reduction operations.
Additional Sink and Source converters
Signature
StreamConverters.javaCollectorStreamConverters.javaCollector
Description
javaCollector creates a SinkSink that accepts a Java 8 java.util.stream.Collector factory. The Collector accumulates incoming elements into a mutable container as downstream demand is triggered, and after the stream completes, an optional finisher transforms the accumulated result. The sink materializes into a FutureCompletionStage holding the final result. All processing happens sequentially on the stream’s materialized execution context.
Since each materialization of the sink must start with a fresh accumulator, the factory is invoked once per materialization. Use javaCollectorParallelUnordered if parallel collection is needed.
Sink.collect provides a simpler API for common collection scenarios.
Example
In this example, StreamConverters.javaCollector uses Collectors.toList to gather stream elements into a List.
- Scala
-
source
val source: Source[String, NotUsed] = Source(List("Apache", "Pekko", "Streams")) val sink: Sink[String, _] = StreamConverters.javaCollector(() => Collectors.toList[String]()) - Java
-
source
Source<String, NotUsed> source = Source.from(Arrays.asList("Apache", "Pekko", "Streams")); CompletionStage<List<String>> result = source.runWith(StreamConverters.javaCollector(Collectors::toList), system);