问题描述
如何将java.util.Map [String,Object]转换为scala.collection.immutable.Map [String,Any],以便将原始地图中的所有值(整数,布尔等)转换为正确的价值在Scala中运作良好。
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.
推荐答案
正如VonC所说, scala.collections.JavaConversion
支持可变集合但是,您不必使用单独的库。可变集合派生自 TraversableOnce
,它定义了返回不可变Map的 toMap
方法:
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)
这将输出
Map(Foo -> true, Bar -> 1)
这篇关于将java.util.Map [String,Object]转换为scala.collection.immutable.Map [String,Any]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!