Q:

Getting value from Java raw map

I'm trying to write a function 
def convert(x: String): String = ...

that converts its argument by looking up in a map, and returning x if the argument isn't a key in the map.  Unfortunately, the map must be obtained by calling a Java method that returns a raw Map:
public static Map getMapFromConfiguration() { ... }

This is legacy code I can't do anything about.  In this particular case, the keys and values of the returned map will be String (and it's OK if the code throws an exception if the keys or values have some other type).  So far, the only way I've been able to get the code to compile is by constructing a new Map[String,String]:
def convert(x: String): String = {
    val mapFromConfig: scala.collection.mutable.Map[_,_] = JavaClass.getMapFromConfiguration().asScala
    val convertedMap: Map[String,String] = (mapFromConfig map {
        case (key, value) => key.asInstanceOf[String] -> value.asInstanceOf[String] }).toMap
    convertedMap.getOrElse(x, x)
}

I've tried various ways to use get or getOrElse directly on mapFromConfig, but haven't found anything that compiles.  Some attempts:
    val mapFromConfig: scala.collection.mutable.Map[_ <: Any, _ <: Any] = JavaClass.getMapFromConfiguration().asScala
    mapFromConfig.getOrElse(x, x).asInstanceOf[String]

Type mismtach, expected: _$1, actual: String

    val mapFromConfig: scala.collection.mutable.Map[Any,Any] = JavaClass.getMapFromConfiguration().asScala
    mapFromConfig.getOrElse(x, x).asInstanceOf[String]

error: type mismatch;
 found   : scala.collection.mutable.Map[?0,?1] where type ?1, type ?0
 required: scala.collection.mutable.Map[Any,Any]
Note: ?0 <: Any, but trait Map is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
Note: ?1 <: Any, but trait Map is invariant in type B.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)

    val mapFromConfig: java.util.Map[_,_] = JavaClass.getMapFromConfiguration()
    mapFromConfig.getOrDefault(x, x).asInstanceOf[String]

Type mismtach, expected: _$2, actual: String

Is there a good way to accomplish what I need without creating an intermediate map?

A:

The main trick is to cast a non-generic Map to explicitly generic java.util.Map[String, String]. Code that works for me is
val map: java.util.Map[String, String] = ScalaToJava.getMapFromConfiguration.asInstanceOf[java.util.Map[String, String]]
println(map.get("key"))

Given Java code:
public class ScalaToJava {
    static Map getMapFromConfiguration() {
        HashMap map = new HashMap();
        map.put("key", "value");
        return map;
    }
}

Or if you want to convert map to the Scala one, you can use something like
val map = ScalaToJava.getMapFromConfiguration.asInstanceOf[java.util.Map[String, String]].asScala
println(map.get("key"))

