score:88
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):
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!
And one that's more like an implicit implicit: :)
import scala.collection.JavaConversions._
val myJavaIterable = someExpr()
for (magicValue <- myJavaIterable) yield doStuffWith(magicValue)
score:6
Starting Scala 2.13
, package scala.jdk.CollectionConverters
replaces deprecated packages scala.collection.JavaConverters/JavaConversions
:
import scala.jdk.CollectionConverters._
// val javaIterable: java.lang.Iterable[Int] = Iterable(1, 2, 3).asJava
javaIterable.asScala
// Iterable[Int] = List(1, 2, 3)
score:11
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
}
Source: stackoverflow.com
Related Query
- How can I convert a Java Iterable to a Scala Iterable?
- How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion
- How to convert a Scala Iterable into Java util.List?
- How do I can convert Scala for loop into Java
- How to convert a Java Stream to a Scala Stream?
- How can I convert a json string to a scala map?
- How to convert a nested scala collection to a nested Java collection
- How to convert a Scala Array[Byte] to Java byte[]?
- How to convert a scala Int to a java Integer?
- How do I convert a Java byte array into a Scala byte array?
- How to declare scala method so that it can be called from Java using varargs style
- How can I convert scala Map to a partial function
- How to convert Scala List to Java ArrayList
- How can I convert a Java map of maps for use in Scala?
- How to convert scala Array to Java array []
- How to convert java Map to scala Map of type LinkedHashMap[String,ArrayList[String]]?
- Using IntelliJ, how can i determine whether particular function stems from Java or Scala
- How to convert a java Map to immutable Scala map via java code?
- How to convert a collection conversion using java to scala
- How can i compile java record with scala code?
- How to convert Scala collection Seq[(Int, Seq[String])] to Java collection List[(int, List[String])]?
- How to convert a Java Collection/List to a Scala seq?
- How to convert a Java TreeMap to a Scala SortedMap?
- Scala - How to convert a Dataset[Row] to a column that can be added to a Dataframe
- How to convert Java LinkedHashMap to Scala LinkedHashMap?
- How to convert Scala Array to Java List ?
- How to convert Java foreach loop in a Scala equivalent?
- Java - How to convert Scala Stream to Java Stream?
- How to convert Java Collection[Int] to Array[Int] in Scala
- How to convert Java Map of Lists to Scala Map of Lists
More Query from same tag
- writing a GridCacheStore implementation in scala with gridgain
- Spring: Injecting a Scala List
- IntelliJ plugin for cucumber-scala
- What is the combination of flatMap and filter?
- Use groupBy and agg for more than one columns in Spark scakla
- Counting empty values in dataframe: scala spark
- How to write lambda for java.UnaryOperator<T> interface in Scala?
- Request body json decimals removed
- scala lamda with multiple =>
- Howto create a .jar including both sources (.java and .scala) and classes with sbt?
- How to convert Columns to rows in Spark scala or spark sql?
- Scala deserialization of custom scala object with play-json library
- Akka Typed Actors and AKka Http - cannot find ActorRefFactory
- PlayFramework SQL Database error
- How can a method accept a (T <: Seq)[A] and return T[B] -- the same type of sequence with a different type parameter?
- Issue with Kafka stream filtering
- Replace if-without-else in Scala
- SBT package JAR file for use in lib folder of other projects
- Error reading image from scala playn
- Scala, represent pattern of boolean tuple into something else
- Exception: 'writeStream' can be called only on streaming Dataset/DataFrame
- Combine two MultiMaps in Scala
- Reducing options in scala?
- Finding type parameters via reflection in Scala 2.10?
- Base Class properties not written to MongoDB
- adding attribute to session
- Unable to get gremlin scala predicate to work
- How check availability of folders before read in Spark?
- Why is persist used like an action in Holden Karau's book "Learning Spark"?
- filter spark dataframe with row field that is an array of strings