我在Scala和Java之间遇到了编译问题。
我的Java代码需要
java.util.Map<Double, java.lang.Iterable<Foo>>
我的scala代码有一个
Map[Double, Vector[Foo]]
我收到编译错误:
error: type mismatch;
found : scala.collection.immutable.Map[scala.Double,Vector[Foo]
required: java.util.Map[java.lang.Double,java.lang.Iterable[Foo]]
scala.collection.JavaConversions似乎不适用于嵌套集合,即使Vector可以被隐式转换为Iterable。如果没有迭代scala集合并手动进行转换,我可以做些什么来使这些类型工作?
答案 0 :(得分:7)
scala.collection.JavaConversions
恕我直言。您最好通过scala.collection.JavaConverters
明确说明转换发生的位置和时间。在你的情况下:
import scala.collection.JavaConverters._
type Foo = Int // Just to make it compile
val scalaMap = Map(1.0 -> Vector(1, 2)) // As an example
val javaMap = scalaMap.map {
case (d, v) => d -> v.toIterable.asJava
}.asJava
答案 1 :(得分:3)
我写了这个通用功能,它可以很好地满足我的需求。
def toJava(x: Any): Any = {
import scala.collection.JavaConverters._
x match {
case y: scala.collection.MapLike[_, _, _] =>
y.map { case (d, v) => toJava(d) -> toJava(v) } asJava
case y: scala.collection.SetLike[_,_] =>
y map { item: Any => toJava(item) } asJava
case y: Iterable[_] =>
y.map { item: Any => toJava(item) } asJava
case y: Iterator[_] =>
toJava(y.toIterable)
case _ =>
x
}
}
答案 2 :(得分:3)
这更适合我的需求:
def toJava(m: Any): Any = {
import java.util
import scala.collection.JavaConverters._
m match {
case sm: Map[_, _] => sm.map(kv => (kv._1, toJava(kv._2))).asJava
case sl: Iterable[_] => new util.ArrayList(sl.map( toJava ).asJava.asInstanceOf[util.Collection[_]])
case _ => m
}
}
答案 3 :(得分:0)
如果有人在spark-scala中寻找解决方案,请尝试使用
import org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema
下面, y是嵌套的WrapperArray
y match {
case x : WrappedArray[x] =>
(x.map(f => f match {case z: GenericRowWithSchema => z.mkString(",").toString()
case z:Any => z })).asJavaCollection
case _ => row.get(i).asInstanceOf[Object]
}
上面的代码,有两件事, 1)如果包装器数组具有原始数据类型,则条件case_通过 2)如果包装器数组具有复杂数据类型(比如struts),则执行GenericRowWithSchema。
答案 4 :(得分:0)
所有其他解决方案都是Any
到Any
,对于像Scala这样的强类型语言来说,这是非常糟糕的。
这是一个尽可能保留类型的解决方案:
trait AsJava[T, R] {
def apply(o: T): R
}
object AsJava extends LowPriorityAsJava {
implicit class RecursiveConverter[T](o: T) {
def asJavaRecursive[R](implicit asJava: AsJava[T, R]): R = asJava(o)
}
implicit lazy val longAsJava: AsJava[Long, lang.Long] = new AsJava[Long, lang.Long] {
def apply(o: Long): lang.Long = Long.box(o)
}
implicit lazy val intAsJava: AsJava[Int, lang.Integer] = new AsJava[Int, lang.Integer] {
def apply(o: Int): lang.Integer = Int.box(o)
}
implicit lazy val doubleAsJava: AsJava[Double, lang.Double] = new AsJava[Double, lang.Double] {
def apply(o: Double): lang.Double = Double.box(o)
}
implicit def mapAsJava[K, V, KR, VR](
implicit
keyAsJava: AsJava[K, KR],
valueAsJava: AsJava[V, VR]
): AsJava[Map[K, V], util.Map[KR, VR]] =
new AsJava[Map[K, V], util.Map[KR, VR]] {
def apply(map: Map[K, V]): util.Map[KR, VR] =
map.map { case (k, v) => (keyAsJava(k), valueAsJava(v)) }.asJava
}
implicit def seqAsJava[V, VR](implicit valueAsJava: AsJava[V, VR]): AsJava[Seq[V], util.List[VR]] =
new AsJava[Seq[V], util.List[VR]] {
def apply(seq: Seq[V]): util.List[VR] = seq.map(valueAsJava(_)).asJava
}
implicit def setAsJava[V, VR](implicit valueAsJava: AsJava[V, VR]): AsJava[Set[V], util.Set[VR]] =
new AsJava[Set[V], util.Set[VR]] {
def apply(set: Set[V]): util.Set[VR] = set.map(valueAsJava(_)).asJava
}
implicit lazy val anyAsJava: AsJava[Any, AnyRef] = new AsJava[Any, AnyRef] {
def apply(o: Any): AnyRef = o match {
case x: Map[Any, Any] => mapAsJava(anyAsJava, anyAsJava)(x)
case x: Seq[Any] => seqAsJava(anyAsJava)(x)
case x: Set[Any] => setAsJava(anyAsJava)(x)
case x: Long => longAsJava(x)
case x: Int => intAsJava(x)
case x: Double => doubleAsJava(x)
case x => x.asInstanceOf[AnyRef]
}
}
}
trait LowPriorityAsJava {
implicit def otherAsJava[T]: AsJava[T, T] = new AsJava[T, T] {
def apply(o: T): T = o
}
}
用法:
Seq(Seq.empty[Int]).asJavaRecursive