NavigableMap 有 Scala 版本吗?
在 Java 1.6 中, NavigableMap (以及引入了 NavigableSet) 接口,并且TreeMap 已更新以实现新接口。除此之外,NavigableMap 对于提出诸如“集合中的哪个元素最接近 X?”之类的问题非常有用(请参阅 François Sarradin 的这篇优秀博文 提供了示例和讨论)。
我希望在 Scala 2.8 中找到类似的东西TreeMap 实现,但可惜,似乎并非如此(至少,不是很明显)。是否有另一个类似于 Java 的 NavigableMap 的 Scala 类或特征?如果没有,是否有一些简单的 Scala 习惯用法可以实现。 。
我意识到我可以使用 Java 的 TreeMap,但我想留在 Scala 集合框架中(如果只是为了简单起见)
In Java 1.6, the NavigableMap (and the NavigableSet) interfaces were introduced and TreeMap was updated to implement the new interface. Among other things, NavigableMap is useful for asking questions like "Which element in the collection is closest to X? (see this excellent blog post by François Sarradin for an example and discussion).
I was hoping to find something similar in Scala 2.8's TreeMap implementation, but alas, it doesn't appear to be so (at least, it isn't obvious). Is there another Scala class or trait which is similar to Java's NavigableMap? If not, are there some simple Scala idioms which can be used to achieve something similar?
I realize I can use Java's TreeMap, but I'd like to stay within the Scala collections framework (if only for simplicity).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在不可变集合上,具有反向引用使得集合不可能持久化。因此,人们使用拉链来浏览此类结构。 Anti-xml 有一个在处理 XML 时使用拉链的很好的示例。
On immutable collections, having the back references makes it impossible to make the collection persistent. So, instead, people use zippers to navigate through such structures. Anti-xml has a nice example of using zippers when working with XML.
这里有一个关于这个主题的帖子
看起来
SortedMap
也许能够帮助您实现这一目标,但到目前为止我已经测试过,我不确定它是否是 O(log(n)) 搜索应该的方式:基于定义
来自
和到
按照rangeImpl 看起来这可能是 O(log(n)),但基于实际计时,它似乎线性增长n 的所有合理值。所以我不确定。
Here's a thread on this topic
It seems
SortedMap
might be able to get you part of the way there, but what I've tested so far I'm not sure if it's O(log(n)) the way a search ought to be:Based on the definitions of
from
andto
in terms of rangeImpl it looks like this could be O(log(n)), but based on actually timing it, it appears to grow linearly for all plausible values of n.So I'm not sure.