Is there an easy way to convert a
java.lang.Iterable[_]
to a
Scala.Iterable[_]
?
Is there an easy way to convert a
java.lang.Iterable[_]
to a
Scala.Iterable[_]
?
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 Siterator[T](private val jit: JavaItr[T]) extends Iterator[T] {
def hasNext: Boolean = jit hasNext
def next: T = jit next
}