I’m trying to convert the parameter map from a ServletRequest to a Scala Map in Scala 2.9.0.1:
val params = request.getParameterMap.asInstanceOf[Map[String, Array[String]]]
I’ve imported collection.JavaConversions._, and at runtime this is thrown:
java.lang.ClassCastException: java.util.Collections$UnmodifiableMap cannot be cast to scala.collection.immutable.Map
How about just calling
.toMapon it?Without calling
toMap,JavaConversionsonly lets you implicitly convert to a mutable Scala map:Presumably this is because a Java
Mapis mutable, so it should only be represented in Scala as amutable.Mapuntil you explicitly convert it to animmutable.Map.Note that when you just say
Mapin Scala, you are really talking aboutcollection.immutable.MapsincePredefaliasesMapthat way:So when you say
request.getParameterMap.asInstanceOf[Map[String, Array[String]]], you are really asking Scala to implicitly convert a JavaMapinto Scala’scollection.immutable.Map, which it doesn’t want to do since Java’sMapis mutable.