Convert java.util.stream.Stream to Scala Stream

别等时光非礼了梦想. 提交于 2020-01-01 14:20:20

问题


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)
}

回答1:


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]


来源:https://stackoverflow.com/questions/38486886/convert-java-util-stream-stream-to-scala-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!