你能否分享一下你最优雅和/或最有效的转换方式的意见
java.util.HashMap[
java.lang.String, java.util.ArrayList[
java.util.ArrayList[java.lang.Double]
]
]
(all of the objects are from java.util or java.lang)
Run Code Online (Sandbox Code Playgroud)
至
Map[
String, Array[
Array[Double]
]
]
(all of the objects are from scala)
Run Code Online (Sandbox Code Playgroud)
谢谢
ret*_*nym 13
我并没有声称这一切都很优雅,但它确实有效.我JavaConversions
明确而不是隐式地使用转换来允许类型推断帮助一点.JavaConversions
是Scala 2.8中的新功能.
import collection.JavaConversions._
import java.util.{ArrayList, HashMap}
import collection.mutable.Buffer
val javaMutable = new HashMap[String, ArrayList[ArrayList[Double]]]
val scalaMutable: collection.Map[String, Buffer[Buffer[Double]]] =
asMap(javaMutable).mapValues(asBuffer(_).map(asBuffer(_)))
val scalaImmutable: Map[String, List[List[Double]]] =
Map(asMap(javaMutable).mapValues(asBuffer(_).map(asBuffer(_).toList).toList).toSeq: _*)
Run Code Online (Sandbox Code Playgroud)
更新:这是一种替代方法,它使用implicits将给定的一组转换应用于任意嵌套的结构.
trait ==>>[A, B] extends (A => B) {
def apply(a: A): B
}
object ==>> {
def convert[A, B](a: A)(implicit a2b: A ==>> B): B = a
// the default identity conversion
implicit def Identity_==>>[A] = new (A ==>> A) {
def apply(a: A) = a
}
// import whichever conversions you like from here:
object Conversions {
import java.util.{ArrayList, HashMap}
import collection.mutable.Buffer
import collection.JavaConversions._
implicit def ArrayListToBuffer[T, U](implicit t2u: T ==>> U) = new (ArrayList[T] ==>> Buffer[U]) {
def apply(a: ArrayList[T]) = asBuffer(a).map(t2u)
}
implicit def HashMapToMap[K, V, VV](implicit v2vv: V ==>> VV) = new (HashMap[K, V] ==>> collection.Map[K, VV]) {
def apply(a: java.util.HashMap[K, V]) = asMap(a).mapValues(v2vv)
}
}
}
object test {
def main(args: Array[String]) {
import java.util.{ArrayList, HashMap}
import collection.mutable.Buffer
// some java collections with different nesting
val javaMutable1 = new HashMap[String, ArrayList[ArrayList[Double]]]
val javaMutable2 = new HashMap[String, ArrayList[HashMap[String, ArrayList[ArrayList[Double]]]]]
import ==>>.{convert, Conversions}
// here comes the elegant part!
import Conversions.{HashMapToMap, ArrayListToBuffer}
val scala1 = convert(javaMutable1)
val scala2 = convert(javaMutable2)
// check the types to show that the conversion worked.
scala1: collection.Map[String, Buffer[Buffer[Double]]]
scala2: collection.Map[String, Buffer[collection.Map[String, Buffer[Buffer[Double]]]]]
}
}
Run Code Online (Sandbox Code Playgroud)
执行此操作的方法已从2.7更改为2.8.Retronym的方法适用于2.8.对于2.7,你可以这样使用collections.jcl
:
object Example {
import scala.collection.jcl
// Build the example data structure
val row1 = new java.util.ArrayList[Double]()
val row2 = new java.util.ArrayList[Double]()
val mat = new java.util.ArrayList[java.util.ArrayList[Double]]()
row1.add(1.0) ; row1.add(2.0) ; row2.add(3.0) ; row2.add(4.0)
mat.add(row1) ; mat.add(row2)
val named = new java.util.HashMap[String,java.util.ArrayList[java.util.ArrayList[Double]]]
named.put("matrix",mat)
// This actually does the conversion
def asScala(thing: java.util.HashMap[String,java.util.ArrayList[java.util.ArrayList[Double]]]) = {
Map() ++ (new jcl.HashMap(thing)).map(kv => {
( kv._1 ,
(new jcl.ArrayList(kv._2)).map(al => {
(new jcl.ArrayList(al)).toArray
}).toArray
)
})
}
}
Run Code Online (Sandbox Code Playgroud)
因此,一般的想法是这样的:从外部开始,将Java集合包装在Scala等价物中,然后使用map将所有内容包装在下一级别中.如果要在Scala表示之间进行转换,请在出路时执行此操作(此处,.toArray
结尾处).
在这里你可以看到工作的例子:
scala> Example.named
res0: java.util.HashMap[String,java.util.ArrayList[java.util.ArrayList[Double]]] = {matrix=[[1.0, 2.0], [3.0, 4.0]]}
scala> val sc = Example.asScala(Example.named)
sc: scala.collection.immutable.Map[String,Array[Array[Double]]] = Map(matrix -> Array([D@1ea817f, [D@dbd794))
scala> sc("matrix")(0)
res1: Array[Double] = Array(1.0, 2.0)
scala> sc("matrix")(1)
res2: Array[Double] = Array(3.0, 4.0)
Run Code Online (Sandbox Code Playgroud)