}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
计算HashCode的操作:
/**
-
Computes key.hashCode() and spreads (XORs) higher bits of hash
-
to lower. Because the table uses power-of-two masking, sets of
-
hashes that vary only in bits above the current mask will
-
always collide. (Among known examples are sets of Float keys
-
holding consecutive whole numbers in small tables.) So we
-
apply a transform that spreads the impact of higher bits
-
downward. There is a tradeoff between speed, utility, and
-
quality of bit-spreading. Because many common sets of hashes
-
are already reasonably distributed (so don’t benefit from
-
spreading), and because we use trees to handle large sets of
-
collisions in bins, we just XOR some shifted bits in the
-
cheapest possible way to reduce systematic lossage, as well as
-
to incorporate impact of the highest bits that would otherwise
-
never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
解决冲突的核心逻辑代码:
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
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 != null) { // exist