Frage

I use Scala 2.10.3.
I have result.iterator() returning Iterator<java.util.Map<String, Object>>. (in Java so)

I want to convert it to the Scala equivalent.
I use import scala.collection.JavaConversions._ to try to do the trick.

However it seems that it is unable to take into account type parameters, in this case, it can convert java.util.Iterator to the Scala equivalent, but fails to convert java.util.Map in the Scala equivalent.

Indeed, a compiler error occurs at this line:

val results: Iterator[Map[String, AnyRef]] = result.iterator()

    type mismatch;
     found   : java.util.Iterator[java.util.Map[String,Object]]
     required: scala.collection.Iterator[scala.collection.immutable.Map[String,AnyRef]]
     val results: Iterator[Map[String, AnyRef]] = result.iterator()
                                                                ^

Is there a short way to do the trick?

War es hilfreich?

Lösung

You could explicitly specify what you want to convert using JavaConverters instead of JavaConversions like this:

import scala.collection.JavaConverters._
def javaIt: java.util.Iterator[java.util.Map[String, Object]] = ???

def scalaIt = javaIt.asScala map {_.asScala}
// Iterator[scala.collection.mutable.Map[String,Object]]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top