數據結構:
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;