Edit: the recommended way is to use JavaConverters
and the .asScala
method:
import scala.collection.JavaConverters._
val myScalaMap = myJavaMap.asScala.mapValues(_.asScala)
Note that you will get mutable maps out of this. You can always use .asScala.toMap
if you want immutable ones.
The original answer with JavaConversions
:
The short answer is: call .mapValues
on the outer map to convert the inner map:
import scala.collection.JavaConversions._
val myScalaMap = myJavaMap.mapValues(_.toMap)
.mapValues
forces the conversion or the outer map to a scala Map
and .toMap
forces the conversion of the inner map to a scala (immutable) map. The immutable part isn't strictly necessary, but anyways...
This is very similar to this anwser. Short example:
scala> val a: java.util.Map[String, java.util.Map[String, String]] = new java.util.HashMap[String, java.util.Map[String, String]]
a: java.util.Map[String,java.util.Map[String,String]] = {}
scala> import scala.collection.JavaConversions._
import scala.collection.JavaConversions._
scala> val myScalaMap = a.mapValues(_.toMap)
myScalaMap: scala.collection.Map[String,scala.collection.immutable.Map[String,String]] = Map()