You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Implements Map.put and related methods. * * @param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent if true, don't change existing value * @param evict if false, the table is in creation mode. * @return previous value, or null if none */finalVputVal(inthash, Kkey, Vvalue, booleanonlyIfAbsent,
booleanevict) {
Node<K,V>[] tab; Node<K,V> p; intn, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; Kk;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
elseif (pinstanceofTreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (intbinCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for keyVoldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
returnoldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
returnnull;
}
/** * 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. */staticfinalinthash(Objectkey) {
inth;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
모든 비트가 1인 값으로 & 연산으로 table.size의 index를 결정한다. 다음에 resize가 되도 기존 index를 유지한다.
2 << n 으로 계속 증가하므로 기존 인덱스를 유지할 수 있다. (다시 hash 계산을 할 필요가 없음. 또띠한걸? 여전히 table array 복사 비용은 그대로.), 키를 다시 계산하는게 아니라 기존 해시된 키를 재사용
모든 비트가 1인 값으로 & 연산으로 table.size의 index를 결정한다. 다음에 resize가 되도 기존 index를 유지한다.
2 << n 으로 계속 증가하므로 기존 인덱스를 유지할 수 있다. (다시 hash 계산을 할 필요가 없음. 또띠한걸? 여전히 table array 복사 비용은 그대로.), 키를 다시 계산하는게 아니라 기존 해시된 키를 재사용
2^n만큼 사이즈가 증가하는 것을 볼 수 있다.
ConcurrentHashMap
The text was updated successfully, but these errors were encountered: