Jdk 1.8 hashMapベース
HashMap特性 key、valueは空の です。スレッドが不安定です。 は、急速な失敗(forサイクルでremoveを操作し、put方法でConcerentModifyExceptionを報告します)をサポートします。 HashMapのいくつかの基本的な属性値 loadFactor:ローディング因子は、loadFactorが大きいほど、hashmapの空間利用率が大きくなり、逆に空間利用率が低いほど、デフォルト値は0.75. です。 threshhold:容量しきい値を超えるとhashMapの拡大動作がトリガされます。 capacity:容量値が2のnの二乗値のデフォルト値は16 です。 size:hashMapに含まれるnodeの個数 hashMapのputプロセスは、index値index=(n-1)&hash を計算する。は、indexを取得して、この位置に要素が存在するかどうかを判定し、存在しない場合は新しい要素を作成し、indexに割り当てて、方法を終了する。 存在すれば、keyとhashの値が着信keyと一致するかどうかを判断し、hashが一致していれば、入ってきたifAbsentの値に基づいてvalueの値を変更するかどうかを判断する。方法は終了する 。 keyがhashと一致しない場合は、indexに対応するチェーンテーブルを巡回し、もしチェーンテーブルにkeyとhashの値が入ってきた値と一致していれば、ifAstant値に基づいてvalue値を変更し、この記録がない場合は、チェーンテーブルの最後の に新しいレコードを作成する。チェーンテーブルの長さが設定された値を超えると、チェーン構造データが赤黒数構造 に変換される。、sizeがthress Hold値を超えると、resize動作がトリガされる 。
このようにhash衝突を減らすことができます。nの値は2のn乗であるため、最高位は1を除いて他のビットは0であり、操作時には演算に参加しません。
問題2:マルチスレッドでputメソッドを実行する場合は、デッドサイクルは行われませんが、データが失われる場合がありますか?
マルチスレッド実行の場合、hash衝突が発生するとデータが上書きされ、データが失われます。
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node[] tab; Node p; int n, 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 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)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) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
HashMap resizeプロセス/**
* Initializes or doubles table size. If null, allocates in
* accord with initial capacity target held in field threshold.
* Otherwise, because we are using power-of-two expansion, the
* elements from each bin must either stay at same index, or move
* with a power of two offset in the new table.
*
* @return the table
*/
final Node[] resize() {
Node[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
// Threshold
if (oldCap > 0) {
// Threshold , hashTable,
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// , , Threshold
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
// ,threshold 0.75
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
// threshold
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node[] newTab = (Node[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node e;
if ((e = oldTab[j]) != null) {
// hashTable j
oldTab[j] = null;
// e next , ,
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
// e TreeNode
else if (e instanceof TreeNode)
((TreeNode)e).split(this, newTab, j, oldCap);
// e next ,
else { // preserve order
Node loHead = null, loTail = null;
Node hiHead = null, hiTail = null;
Node next;
do {
next = e.next;
// e.hash 0
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
// e.hash 1, oldCap
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
問題1:なぜ(n-1)&hashでデータの下付き値を計算しますか?このようにhash衝突を減らすことができます。nの値は2のn乗であるため、最高位は1を除いて他のビットは0であり、操作時には演算に参加しません。
問題2:マルチスレッドでputメソッドを実行する場合は、デッドサイクルは行われませんが、データが失われる場合がありますか?
マルチスレッド実行の場合、hash衝突が発生するとデータが上書きされ、データが失われます。