Convert java.util.stream.Stream to Scala Stream

房东的猫 提交于 2019-12-04 15:43:22

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