How do I convert java.util.Map[String, Object] to scala.collection.immutable.Map[String, Any], so that all values in the original map (integers, booleans etc.) are converted to the right value to work well in Scala.
If you just want a mutable HashMap , you can just use x. toMap in 2.8 or collection. immutable. Map(x.
By default, Scala uses the immutable Map. If you want to use the mutable Map, you'll have to import scala. collection. mutable.
Use Object#toString() . String string = map. toString(); That's after all also what System.
In Java, you can use the Jackson library to convert a Java object into a Map easily.
As VonC says, scala.collections.JavaConversion
supports mutable collections only, but you don't have to use a separate library. Mutable collections are derived from TraversableOnce
which defines a toMap
method that returns an immutable Map:
import scala.collection.JavaConversions._ val m = new java.util.HashMap[String, Object]() m.put("Foo", java.lang.Boolean.TRUE) m.put("Bar", java.lang.Integer.valueOf(1)) val m2: Map[String, Any] = m.toMap println(m2)
This will output
Map(Foo -> true, Bar -> 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With