Hashmaps,Treemaps和LinkedHashmaps如何在Java中工作?

Gek*_*kal 2 java hashmap treemap linkedhashmap

我对地图有各种疑问:

  1. 迭代Hashmap时,无法保证迭代顺序.那为什么呢?
  2. 为什么Hashmaps比Treemaps更快?
  3. LinkedHashMaps如何运作,他们如何维护订单?是因为它们有一个双向链表,其中包含有关在条目之前和之后存储哪个条目的信息?

我一直在阅读API文档,但由于我是初学者,在编程方面我很难理解它.

Mak*_*oto 5

迭代Hashmap时,无法保证迭代顺序.那为什么呢?

它们不是按照发生时的顺序插入的,而是它们散列的值.

例如,假设我们有一个哈希函数h(x),它为字符串"Hello"返回127,为"Zebra"返回12.如果我们将这些键输入到我们的哈希映射中,它们被读出的顺序是"Zebra" - >(无论附加什么值),然后是"Hello" - >(无论附加什么值).

这在源代码中很明显HashMap:

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}
Run Code Online (Sandbox Code Playgroud)

请注意,这是对实际执行和表示的哈希的简化变化.可能是哈希值发生冲突,并且需要以某种方式解决冲突.这是指底漆; 密钥是按其哈希的顺序插入的,但是如果你的哈希函数有缺陷或你的对象的哈希没有一个好的值,那么你可能会遇到异常行为.

为什么Hashmaps比Treemaps更快?

哈希操作不依赖于整个集合的大小.回想一下,h(x)仅基于我们试图插入的单个值进行操作.如果我们将元素插入到a中TreeMap,我们必须考虑它们所处的自然顺序 - 这涉及遍历结构以找到要插入的点,并且还可能涉及重新平衡或重新组织结构以确保保持平衡.

有一个很多更重要的来源TreeMapput方法.

public V put(K key, V value) {
    Entry<K,V> t = root;
    if (t == null) {
        compare(key, key); // type (and possibly null) check

        root = new Entry<>(key, value, null);
        size = 1;
        modCount++;
        return null;
    }
    int cmp;
    Entry<K,V> parent;
    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator;
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    else {
        if (key == null)
            throw new NullPointerException();
        @SuppressWarnings("unchecked")
            Comparable<? super K> k = (Comparable<? super K>) key;
        do {
            parent = t;
            cmp = k.compareTo(t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    Entry<K,V> e = new Entry<>(key, value, parent);
    if (cmp < 0)
        parent.left = e;
    else
        parent.right = e;
    fixAfterInsertion(e);
    size++;
    modCount++;
    return null;
}
Run Code Online (Sandbox Code Playgroud)

LinkedHashMaps如何运作,他们如何维护订单?是因为它们有一个双向链表,其中包含有关在条目之前和之后存储哪个条目的信息?

您可以自己阅读来源,但主要内容如下:

  • 密钥仍然以哈希结构组合在一起,以确保它们的唯一性.
  • 它们的插入顺序由FIFO结构保存,如链表.

可以这样想:您使用哈希函数来确保密钥是唯一的,如果是,则立即将其及其值插入列表中.这样,保留了排序和唯一性.