在 TreeMap 中搜索 (Java)
我需要在地图的地图中进行搜索并返回该元素所属的键。 我觉得这个实现很慢,你能帮我优化一下吗? 我需要使用 TreeSet,但不能使用 contains,因为它们使用compareTo,而 equals/compareTo 对以不兼容的方式实现,我无法更改它。 (抱歉我的英语不好)
Map<Key, Map<SubKey, Set<Element>>> m = new TreeSet();
public String getKeys(Element element) {
for(Entry<Key, Map<SubKey, Set<Element>>> e : m.entrySet()) {
mapSubKey = e.getValue();
for(Entry<SubKey, Set<Element>> e2 : mapSubKey.entrySet()) {
setElements = e2.getValue();
for(Element elem : setElements)
if(elem.equals(element)) return "Key: " + e.getKey() + " SubKey: " + e2.getKey();
}
}
}
I need to do a search in a map of maps and return the keys this element belong.
I think this implementation is very slow, can you help me to optimize it?.
I need to use TreeSet and I can't use contains because they use compareTo, and equals/compareTo pair are implemented in an incompatible way and I can't change that.
(sorry my bad english)
Map<Key, Map<SubKey, Set<Element>>> m = new TreeSet();
public String getKeys(Element element) {
for(Entry<Key, Map<SubKey, Set<Element>>> e : m.entrySet()) {
mapSubKey = e.getValue();
for(Entry<SubKey, Set<Element>> e2 : mapSubKey.entrySet()) {
setElements = e2.getValue();
for(Element elem : setElements)
if(elem.equals(element)) return "Key: " + e.getKey() + " SubKey: " + e2.getKey();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的问题是键和值是向后的。
映射允许人们高效地查找与键(本例中为
Element
)关联的值(即Key
和SubKey
)。向后走是缓慢的。
有双向地图实现,例如 Google Collections BiMap, 支持双向更快的访问,但这意味着替换
TreeMap
。否则,维护两张地图,每个方向一张。The problem here is that the keys and values are backward.
Maps allow one to efficiently find a value (which would be
Key
andSubKey
) associated with a key (Element
, in this example).Going backwards is slow.
There are bi-directional map implementations, like Google Collections BiMap, that support faster access in both directions—but that would mean replacing
TreeMap
. Otherwise, maintain two maps, one for each direction.如果您无法使用
contains
,并且您被困在使用 Map of Maps 中,那么您唯一真正的选择就是迭代,就像您正在做的那样。或者,您可以在单独的映射中保留
Element
到Key/SubKey
的反向映射,这将使反向查找更快。另外,如果您不确定给定的
Element
只能存在于一个位置,您可能需要存储和检索List
而不仅仅是 <代码>元素if you can't use
contains
, and you're stuck using a Map of Maps, then your only real option is to iterate, as you are doing.alternatively, you could keep a reverse map of
Element
toKey/SubKey
in a separate map, which would make reverse lookups faster.also, if you're not sure that a given
Element
can exist in only one place, you might want to store and retrieve aList<Element>
instead of just anElement
使用 TreeMap 和 TreeSet 在 compareTo 和 equals 的实现方式是:彼此兼容。此外,当在 Map 中搜索时,只有对键的搜索才是有效的(对于 TreeMap O(log n))。当在 Map 中搜索值时,复杂度将变为线性。
有一种方法可以优化内部 TreeSet 中的搜索 不过,在实现您自己的 Comparator< /a> 用于元素类型。这样您就可以实现自己的compareTo 方法,而无需更改Element 对象本身。
Using TreeMap and TreeSet work properly when compareTo and equals are implemented in such a way that they are compatible with each other. Furthermore, when searching in a Map, only the search for the key will be efficient (for a TreeMap O(log n)). When searching for a value in a Map, the complexity will become linear.
There is a way to optimize the search in the inner TreeSet though, when implementing your own Comparator for the Element type. This way you can implement your own compareTo method without changing the Element object itself.
这是一个双向 TreeMap(或 TreeMap 上的双射)。
它将两个重载的 TreeMap 关联在一起,并将它们捆绑在一起。
每个“逆”常量字段都指向另一个 TreeMap。一个 TreeMap 上的任何更改都会自动反映在其逆图中。
因此,每个值都是唯一的。
Here is a bidirectional TreeMap (or Bijection over TreeMap).
It associates two overloaded TreeMaps Which are tied together.
Each one "inverse" constant field points to the other TreeMap. Any change on one TreeMap is automatically reflected on its inverse.
As a result, each value is unique.