Arne Arne - 1 year ago 127
Scala Question

Convert 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

to convert a
to a

I would like to do something like this:

def streamFiles(path: Path): Stream[Path] = {

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 ) #:: loop(it)

Answer Source

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 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, 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] = {

But if you still want to convert a 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)[Stream]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download