Scala Java 深度映射转换(ConcurrentHashMap)

发布于 2024-11-01 21:01:51 字数 718 浏览 2 评论 0原文

我有一个 ConcurrentHashMap 对象定义如下:

import scala.collection.JavaConversions._
val storage: ConcurrentMap[String,ConcurrentMap[Int,ConcurrentMap[String,Double]]] =
new ConcurrentHashMap[String,ConcurrentHashMap[Int,ConcurrentHashMap[String,Double]]]

Scala (2.8.1) 在编译时抱怨以下错误:

found   : java.util.concurrent.ConcurrentHashMap[String,java.util.concurrent.ConcurrentHashMap[String,String]]
required: scala.collection.mutable.ConcurrentMap[String,scala.collection.mutable.ConcurrentMap[String,String]]

但是当我尝试以下代码时,它起作用了:

val storage: ConcurrentMap[String,Double] = new ConcurrentHashMap[String,Double]

我感谢您对如何修复此错误的评论。

I have a ConcurrentHashMap object defined as below:

import scala.collection.JavaConversions._
val storage: ConcurrentMap[String,ConcurrentMap[Int,ConcurrentMap[String,Double]]] =
new ConcurrentHashMap[String,ConcurrentHashMap[Int,ConcurrentHashMap[String,Double]]]

And Scala (2.8.1) complains at compilation with the following error:

found   : java.util.concurrent.ConcurrentHashMap[String,java.util.concurrent.ConcurrentHashMap[String,String]]
required: scala.collection.mutable.ConcurrentMap[String,scala.collection.mutable.ConcurrentMap[String,String]]

But when I try the following code, it works:

val storage: ConcurrentMap[String,Double] = new ConcurrentHashMap[String,Double]

I appreciate your comment how to fix this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-11-08 21:01:51

隐式转换(例如 JavaConversions 提供的转换)在实例级别而不是类型级别工作。因此,虽然最外层的地图可以转换,但内部的地图则不能。

没有办法直接隐式转换嵌套的情况。而且,不幸的是,在以下示例中似乎没有正确选择最具体的隐式:

scala> val storage = new ConcurrentHashMap[String,ConcurrentHashMap[Int,ConcurrentHashMap[String,Double]]]
storage: java.util.concurrent.ConcurrentHashMap[String,java.util.concurrent.ConcurrentHashMap[Int,java.util.concurrent.ConcurrentHashMap[String,Double]]] = {}

scala> import scala.collection.JavaConverters._

scala> for ((i, m) <- storage.asScala; (j, m2) <- m.asScala; (k, l) <- m2.asScala) yield ((i, j) -> (k, l))
res0: scala.collection.mutable.Map[(String, Int),(String, Double)] = Map()

scala> import scala.collection.JavaConversions._

scala> for ((i, m) <- storage; (j, m2) <- m; (k, l) <- m2) yield ((i, j) -> (k, l))                        
res3: scala.collection.mutable.Map[(String, Int),(String, Double)] = Map()

因此,在最终分析中,我预测将来会出现很多类型注释(上面的 for 理解可能会与显式类型归属一起使用,但我太困了,无法把所有内容都打出来。)

Implicit conversions, such as the one provided by JavaConversions, work at the instance level rather than the type level. So, while the outermost map can be converted, the inner maps will not be.

There isn't a way to directly have the nested case implicitly converted. And, unfortunately, it appears that the most specific implicit is not being correctly selected in the following examples:

scala> val storage = new ConcurrentHashMap[String,ConcurrentHashMap[Int,ConcurrentHashMap[String,Double]]]
storage: java.util.concurrent.ConcurrentHashMap[String,java.util.concurrent.ConcurrentHashMap[Int,java.util.concurrent.ConcurrentHashMap[String,Double]]] = {}

scala> import scala.collection.JavaConverters._

scala> for ((i, m) <- storage.asScala; (j, m2) <- m.asScala; (k, l) <- m2.asScala) yield ((i, j) -> (k, l))
res0: scala.collection.mutable.Map[(String, Int),(String, Double)] = Map()

scala> import scala.collection.JavaConversions._

scala> for ((i, m) <- storage; (j, m2) <- m; (k, l) <- m2) yield ((i, j) -> (k, l))                        
res3: scala.collection.mutable.Map[(String, Int),(String, Double)] = Map()

So, in final analysis, I predict a lot of type annotations in your future (the for comprehensions above would probably work with explicit type ascription, but I'm too sleepy to type it all out.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文