hashmap在jdk7和jdk8下的区别

数据结构:
jdk1.7是数组 + 链表
jdk1.8是数据 + 链表 + 红黑树

key的hash计算
jdk1.7
将key的hashCode无符号右移后做异或运算

        h ^= k.hashCode();

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);

jdk1.8
key的hashCode无符号右移16位结果同hashCode做异或运算,即高16位不变,低16位和高16位做异或运算最终得到hash,这样hash结果冲突概率更小。

    static final int hash(Object key) {
    
    
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

hash冲突解决
hash冲突即key的hash值一样但是value不一致的情况。
jdk1.7
遍历table数组后在表头插入新元素。

for (Entry<K,V> e = table[i]; e != null; e = e.next) {
    
    
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
    
    
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);

hash冲突后会执行到addEntry方法,里面最终会调以下方法,table[bucketIndex] = new Entry<>(hash, key, value, e); 这句会把新元素插入到原来链表的表头。

void createEntry(int hash, K key, V value, int bucketIndex) {
    
    
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<>(hash, key, value, e);
        size++;
    }

jdk1.8
在putVal方法中

for (int binCount = 0; ; ++binCount) {
    
    
   if ((e = p.next) == null) {
    
    
    p.next = newNode(hash, key, value, null);
    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
       treeifyBin(tab, hash);
         break;
         }
      if (e.hash == hash &&
       ((k = e.key) == key || (key != null && key.equals(k))))
           break;
             p = e;
        }

if ((e = p.next) == null)如果满足,则会往链表的尾部插入新元素。

扩容
jdk1.7
如果table的数组长度超过threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); 会触发扩容,扩容后长度为原数组长度的两倍。

jdk1.8
触发条件跟1.7一样。

jdk1.8链表转红黑树条件:链表长度达到8并且table数组的长度(桶数量)达到最小树形化阈值static final int MIN_TREEIFY_CAPACITY = 64;

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/huangdi1309/article/details/106411532