Java - How to convert Scala Stream to Java Stream?

I have a Java application that receives a Scala Stream.

I need to convert this to a Java Stream.

How can I do this in Java?


This is a bit of a long way around, but you can convert the scala stream to an iterable, to a java iterable, then construct the java stream from the java iterable:

import scala.collection.JavaConverters._

def stream2javaStream[T](scalaStream: scala.Stream[T]):[T] = {, false);

stream2javaStream((1 to 100).toStream)

res0:[Int] =$Head@2489e84a

While roundabout, this does not "realize" the stream, maintaining its efficiency.

stream2javaStream((1 to 100){i => println(i); i})
res1:[Int] =$Head@9b21bd3

only prints 1 for the head of the stream

If you don't want to process the stream in parallel you can simply subclass AbstractSpliterator:

import java.util.function.Consumer
import java.util.{Spliterator, Spliterators}

def streamToJava[T](s: Stream[T]):[T] = {
    var ss: Stream[T] = s
    val splitr = new Spliterators.AbstractSpliterator[T](Long.MaxValue, Spliterator.IMMUTABLE) {
      override def tryAdvance(action: Consumer[_ >: T]): Boolean = ss match {
        case Stream.Empty => false
        case h #:: tl => {ss = tl; action.accept(h); true}
    }, false)

The other answer (I'm too new to comment) is incorrect as it calls size which materialises the entire stream.

Starting Scala 2.13, the standard library includes scala.jdk.StreamConverters which provides Java to Scala implicit stream conversions:

import scala.jdk.StreamConverters;

// val scalaStream = Stream(1, 2, 3)
//[Int] =$Head@3cccf515

Note that Streams are renamed LazyList in Scala 2.13, in which case:

// val scalaLazyList = LazyList(1, 2, 3)
//[Int] =$Head@4997c13

Need Your Help