Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert java.util.stream.Stream to Scala Stream

I know how I can use the Java libraries, and I can write some loops that do the stuff I need for me, but the question is more, why is there nothing in scala.collection.JavaConverters or scala.collection.JavaConverstions to convert a java.util.stream.Stream to a scala.collection.immutable.Stream?

I would like to do something like this:

def streamFiles(path: Path): Stream[Path] = {
    Files.newDirectoryStream(path).asScala
}

But instead I have to write something like this:

def streamFiles(path: Path): Stream[Path] = {
  val path_it : java.util.Iterator[Path] = Files.newDirectoryStream(path).iterator()
  def loop(it: java.util.Iterator[Path]): Stream[Path] =
    if( it.hasNext )
     it.next #:: loop(it)
    else
      Stream()
  loop(path_it)
}
like image 499
Arne Avatar asked Dec 18 '22 15:12

Arne


1 Answers

The current version of JavaConverters and JavaConversions don't offer conversions between Java Stream and Scala Stream because of differences at roadmaps.

Java 8, the version where java.util.stream.Stream was introduced, was released at Mar 2014 while Scala 2.11.0 was officially released at Apr 2014. So there was no time to change Scala 2.11 plans to add better integration with Java 8 features. In fact, better support to Java 8 is planned for Scala 2.12 (see the roadmap here and also this presentation from Adrian Moors).

But anyway, there is scala-java8-compat, "a Java 8 compatibility kit for Scala" and there is also experimental support in Scala 2.11.

Looking at your code, there is a relevant issue: Files.newDirectoryStream returns a DirectoryStream which is not a sub interface of java.util.stream.Stream, but an Iterable instead. So, what you need is a way to convert an Iterable to a Scala Stream, if this is what you really want, and your code should be like:

import java.nio.file.Path
import java.nio.file.Files

import scala.collection.JavaConverters._

object Foo {
  def streamFiles(path: Path): Stream[Path] = {
    Files.newDirectoryStream(path).iterator().asScala.toStream
  }
}

But if you still want to convert a java.util.stream.Stream to Scala Stream, add scala-java8-compat version 0.8.0-RC3 or newer to your project (you may need to add a new resolver to your build too) and then do something like:

import scala.compat.java8.StreamConverters._

val numbers: java.util.List[Int] = java.util.Arrays.asList(1, 2, 3)
numbers.stream().toScala[Stream]
like image 126
marcospereira Avatar answered Dec 27 '22 20:12

marcospereira