import scala.collection.JavaConverters._
scala> val m = new java.util.LinkedHashMap[String,Int]
m: java.util.LinkedHashMap[String,Int] = {}
scala> m.asScala
res0: scala.collection.mutable.Map[String,Int] = Map()
scala> m.asScala.getClass
res1: Class[_ <: scala.collection.mutable.Map[String,Int]] = class scala.collection.convert.Wrappers$JMapWrapper
mutable
のほうの Map に変換される。
scala> m.put("e",5)
res3: Int = 0
scala> m.put("a",9)
res4: Int = 0
scala> m.put("b", 1)
res5: Int = 0
scala> m.asScala
res6: scala.collection.mutable.Map[String,Int] = Map(e -> 5, a -> 9, b -> 1)
scala> m.asScala.toArray
res7: Array[(String, Int)] = Array((e,5), (a,9), (b,1))
asScala
では順序は保持されている。
How to convert Java LinkedHashMap to Scala LinkedHashMap? - Stack Overflow
明示的に mutable.LinkedHashMap
をつくりたい場合
scala> val l = scala.collection.mutable.LinkedHashMap[String,Int](m.asScala.toArray : _*)
l: scala.collection.mutable.LinkedHashMap[String,Int] = Map(e -> 5, a -> 9, b -> 1)
scala> l.getClass
res10: Class[_ <: scala.collection.mutable.LinkedHashMap[String,Int]] = class scala.collection.mutable.LinkedHashMap