如何将Java Iterable转换为Scala Iterable?

时间:2022-11-25 19:38:48

Is there an easy way to convert a

是否有一种简单的方法来转换

java.lang.Iterable[_]

to a

Scala.Iterable[_]

?

2 个解决方案

#1


In Scala 2.8 this became much much easier, and there are two ways to achieve it. One that's sort of explicit (although it uses implicits):

在Scala 2.8中,这变得容易得多,并且有两种方法可以实现它。一个明确的(尽管它使用含义):

import scala.collection.JavaConverters._

val myJavaIterable = someExpr()

val myScalaIterable = myJavaIterable.asScala

EDIT: Since I wrote this, the Scala community has arrived at a broad consensus that JavaConverters is good, and JavaConversions is bad, because of the potential for spooky-action-at-a-distance. So don't use JavaConversions at all!

编辑:自从我写这篇文章以来,Scala社区达成了一个广泛的共识,即JavaConverters很好,而且JavaConversions很糟糕,因为有可能在远距离采取怪异行动。所以不要使用JavaConversions!


And one that's more like an implicit implicit: :)

而且更像是一个隐含的暗示::)

import scala.collection.JavaConversions._

val myJavaIterable = someExpr()

for (magicValue <- myJavaIterable) yield doStuffWith(magicValue)

#2


Yes use implicit conversions:

是使用隐式转换:

import java.lang.{Iterable => JavaItb}
import java.util.{Iterator => JavaItr}

implicit def jitb2sitb[T](jit: JavaItb[T]): Iterable[T] = new SJIterable(jit);
implicit def jitr2sitr[A](jit: JavaItr[A]): Iterator[A] = new SJIterator(jit)

Which can then be easily implemented:

然后可以轻松实现:

class SJIterable[T](private val jitb: JavaItr[T]) extends Iterable[T] {
  def elements(): Iterator[T] = jitb.iterator()
}

class SJIterator[T](private val jit: JavaItr[T]) extends Iterator[T] {
  def hasNext: Boolean = jit hasNext

  def next: T = jit next
}

#1


In Scala 2.8 this became much much easier, and there are two ways to achieve it. One that's sort of explicit (although it uses implicits):

在Scala 2.8中,这变得容易得多,并且有两种方法可以实现它。一个明确的(尽管它使用含义):

import scala.collection.JavaConverters._

val myJavaIterable = someExpr()

val myScalaIterable = myJavaIterable.asScala

EDIT: Since I wrote this, the Scala community has arrived at a broad consensus that JavaConverters is good, and JavaConversions is bad, because of the potential for spooky-action-at-a-distance. So don't use JavaConversions at all!

编辑:自从我写这篇文章以来,Scala社区达成了一个广泛的共识,即JavaConverters很好,而且JavaConversions很糟糕,因为有可能在远距离采取怪异行动。所以不要使用JavaConversions!


And one that's more like an implicit implicit: :)

而且更像是一个隐含的暗示::)

import scala.collection.JavaConversions._

val myJavaIterable = someExpr()

for (magicValue <- myJavaIterable) yield doStuffWith(magicValue)

#2


Yes use implicit conversions:

是使用隐式转换:

import java.lang.{Iterable => JavaItb}
import java.util.{Iterator => JavaItr}

implicit def jitb2sitb[T](jit: JavaItb[T]): Iterable[T] = new SJIterable(jit);
implicit def jitr2sitr[A](jit: JavaItr[A]): Iterator[A] = new SJIterator(jit)

Which can then be easily implemented:

然后可以轻松实现:

class SJIterable[T](private val jitb: JavaItr[T]) extends Iterable[T] {
  def elements(): Iterator[T] = jitb.iterator()
}

class SJIterator[T](private val jit: JavaItr[T]) extends Iterator[T] {
  def hasNext: Boolean = jit hasNext

  def next: T = jit next
}