Java SortedMap 到 Scala TreeMap
我在将 java SortedMap 转换为 scala TreeMap 时遇到问题。 SortedMap来自反序列化,在使用之前需要转换为scala结构。
出于好奇,一些背景是序列化结构是通过 XStream 编写的,并且在反序列化时我注册了一个转换器,该转换器表示应该给出可以分配给 SortedMap[Comparable[_],_]
的任何内容大部头书。因此,我的 Convert 方法被调用,并被赋予一个可以安全地转换的 Object
,因为我知道它的类型为 SortedMap[Comparable[_],_]
。这就是有趣的地方。这里有一些示例代码可能有助于解释它。
// a conversion from comparable to ordering
scala> implicit def comparable2ordering[A <: Comparable[A]](x: A): Ordering[A] = new Ordering[A] {
| def compare(x: A, y: A) = x.compareTo(y)
| }
comparable2ordering: [A <: java.lang.Comparable[A]](x: A)Ordering[A]
// jm is how I see the map in the converter. Just as an object. I know the key
// is of type Comparable[_]
scala> val jm : Object = new java.util.TreeMap[Comparable[_], String]()
jm: java.lang.Object = {}
// It's safe to cast as the converter only gets called for SortedMap[Comparable[_],_]
scala> val b = jm.asInstanceOf[java.util.SortedMap[Comparable[_],_]]
b: java.util.SortedMap[java.lang.Comparable[_], _] = {}
// Now I want to convert this to a tree map
scala> collection.immutable.TreeMap() ++ (for(k <- b.keySet) yield { (k, b.get(k)) })
<console>:15: error: diverging implicit expansion for type Ordering[A]
starting with method Tuple9 in object Ordering
collection.immutable.TreeMap() ++ (for(k <- b.keySet) yield { (k, b.get(k)) })
I'm having trouble converting a java SortedMap into a scala TreeMap. The SortedMap comes from deserialization and needs to be converted into a scala structure before being used.
Some background, for the curious, is that the serialized structure is written through XStream and on desializing I register a converter that says anything that can be assigned to SortedMap[Comparable[_],_]
should be given to me. So my convert method gets called and is given an Object
that I can safely cast because I know it's of type SortedMap[Comparable[_],_]
. That's where it gets interesting. Here's some sample code that might help explain it.
// a conversion from comparable to ordering
scala> implicit def comparable2ordering[A <: Comparable[A]](x: A): Ordering[A] = new Ordering[A] {
| def compare(x: A, y: A) = x.compareTo(y)
| }
comparable2ordering: [A <: java.lang.Comparable[A]](x: A)Ordering[A]
// jm is how I see the map in the converter. Just as an object. I know the key
// is of type Comparable[_]
scala> val jm : Object = new java.util.TreeMap[Comparable[_], String]()
jm: java.lang.Object = {}
// It's safe to cast as the converter only gets called for SortedMap[Comparable[_],_]
scala> val b = jm.asInstanceOf[java.util.SortedMap[Comparable[_],_]]
b: java.util.SortedMap[java.lang.Comparable[_], _] = {}
// Now I want to convert this to a tree map
scala> collection.immutable.TreeMap() ++ (for(k <- b.keySet) yield { (k, b.get(k)) })
<console>:15: error: diverging implicit expansion for type Ordering[A]
starting with method Tuple9 in object Ordering
collection.immutable.TreeMap() ++ (for(k <- b.keySet) yield { (k, b.get(k)) })
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,澄清您的错误:
您可以编写一个隐式将
Comparable[T]
视为Ordering[T]
,如下所示。但是,如果您确实不知道键的类型,我认为您无法根据
Comparable#compareTo
创建Ordering
,至少不需要反射:Firstly, to clarify your error:
You can write an implicit to treat
Comparable[T]
asOrdering[T]
as follows.However, if you really don't know the type of the key, I don't think you can create the
Ordering
in terms ofComparable#compareTo
, at least without reflection:您也可以只为 TreeMap 指定一个显式类型。这就是我刚刚解决类似问题的方法:(
抱歉,我没有时间检查这到底如何适用于问题中发布的来源。)
You can probably also just give an explicit type to the TreeMap. That's how I just solved a similar problem:
(Sorry, I don't have the time to check how exactly this applies to the sources posted in the question.)