How to Convert Map's Iterator from Java to Scala using JavaConversions

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?

Answers


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]]

Need Your Help

How to include <script type> in external .js file?

javascript external

I want to include following JS in external .JS file.