StreamConverters.fromJavaStream

Create a source that wraps a Java 8 java.util.stream.Stream.

Additional Sink and Source converters

Signature

StreamConvertersStreamConverters

Example

Here is an example of a SourceSource created from a java.util.stream.Stream.

Scala
sourceimport java.util.stream
import java.util.stream.IntStream

import akka.NotUsed
import akka.stream.scaladsl.Keep
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
import akka.stream.scaladsl.StreamConverters
def factory(): IntStream = IntStream.rangeClosed(0, 9)
val source: Source[Int, NotUsed] = StreamConverters.fromJavaStream(() => factory()).map(_.intValue())
val sink: Sink[Int, Future[immutable.Seq[Int]]] = Sink.seq[Int]

val futureInts: Future[immutable.Seq[Int]] = source.toMat(sink)(Keep.right).run()
Java
sourceimport akka.japi.function.Creator;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.StreamConverters;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.BaseStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Creator<BaseStream<Integer, IntStream>> creator = () -> IntStream.rangeClosed(0, 9);
Source<Integer, NotUsed> source = StreamConverters.fromJavaStream(creator);

Sink<Integer, CompletionStage<Integer>> sink = Sink.last();

CompletionStage<Integer> integerCompletionStage = 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.