简体   繁体   English

Java树形结构中的HashMap

[英]HashMap in Tree structure in Java

I am looking for a container that would basically work like HashMap where I could put and get any of the entries in O(1) time. 我正在寻找一个基本上可以像HashMap一样工作的容器,我可以在其中放置并获取O(1)时间中的任何条目。 I also want to be able to iterate through but I want the order to be sorted by values. 我还希望能够进行迭代,但是我希望按值对顺序进行排序。 So neither TreeMap nor LinkedHashMap would work for me. 所以TreeMapLinkedHashMap都不适合我。 I found the example below: 我发现以下示例:

SortedSet<Map.Entry<String, Double>> sortedSet = new TreeSet<Map.Entry<String, Double>>(
        new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> e1,
                    Map.Entry<String, Double> e2) {
                return e1.getValue().compareTo(e2.getValue());
            }});

The problem is SortedSet doesn't have any get method to get entries. 问题是SortedSet没有任何get方法来获取条目。 I will be using this collection in a place where I will be adding entries but in case of already existing entry, the value(double) will be updated and then sorted again by using the comparator(which compares values as mentioned above). 我将在将要添加条目的地方使用此集合,但是如果已有条目,则将更新value(double),然后使用比较器(如上所述比较值)再次对其进行排序。 What can I use for my needs? 我可以用来满足我的需求吗?

There is no such data structure in the Java class libraries. Java类库中没有这样的数据结构。

But you could create one that is a wrapper for a private HashMap and a private TreeMap with the same set of key/value pairs. 但是您可以创建一个作为私有HashMap和私有TreeMap的包装的具有相同键/值对集合的包装。

This gives a data structure that has get complexity of O(1) like a regular HashMap (but not put or other update operations), and a key Set and entry Set which can be iterated in key order ... as requested in the original version of this Question. 这给具有数据结构get的复杂性O(1)像一个普通HashMap (但不put它可在关键的顺序重复或其他更新操作),以及一键设定和条目集...作为原请求这个问题的版本。


Here is a start for you: 这是您的起点:

public class HybridMap<K,V> implements Map<K,V> {
    private HashMap<K,V> hashmap = ...
    private TreeMap<K,V> treemap = ...

    @Override V get(K key) {
        return hashmap.get(key);
    }

    @Override void (K key, V value) {
        hashmap.put(key, value);
        treemap.put(key, value);
    }

    // etcetera
}

Obviously, I've only implemented some of the (easy) methods. 显然,我只实现了一些(简单)方法。


If you want to be able to iterate the entries in value order (rather than key order), once again there is no such data structure in the Java class libraries. 如果您希望能够按值顺序(而不是键顺序)对条目进行迭代,那么Java类库中再也没有这样的数据结构。

In this case, wrapping a HashMap and TreeMap is going to be very complicated if you need the resulting Map to conform fully to the API contract. 在这种情况下,如果您需要生成的Map完全符合API合同,则包装HashMapTreeMap将会非常复杂。

So I suggest that you just use a HashMap and TreeSet of the key/value pairs ... and manually keep them in step. 因此,我建议您只使用键/值对的HashMap和TreeSet ...并手动使它们保持同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM