HashMap_putメソッド実装解析
31873 ワード
HashMapの下位層は解析を実現し,jdk 14を用いた.
1 hashmap内部ノード(内部クラス)
2 putメソッド
3関連する他の方法
3.1 resize()
1 hashmap内部ノード(内部クラス)
/**
* Basic hash bin node, used for most entries. (See below for
* TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
*/
static class Node<K,V> implements Map.Entry<K,V> {
// key hash
final int hash;
// key final , key
final K key;
V value;
// ,
Node<K,V> next;
//
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
// hashCode
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
// ,
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
// equals
public final boolean equals(Object o) {
if (o == this)
return true;
//
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
//
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
// true
return true;
}
return false;
}
}
2 putメソッド
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with {@code key}, or
* {@code null} if there was no mapping for {@code key}.
* (A {@code null} return can also indicate that the map
* previously associated {@code null} with {@code key}.)
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* 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
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
// tab ;
// p
// n
// i
Node<K,V>[] tab; Node<K,V> p; int n, i;
// tab = table
// n = tab.length
// 0
if ((tab = table) == null || (n = tab.length) == 0)
//
n = (tab = resize()).length;
// i = (n - 1) & hash
// p = tab[i] ( )
//
if ((p = tab[i = (n - 1) & hash]) == null)
//
// i
tab[i] = newNode(hash, key, value, null);
else {
// e
// k
Node<K,V> e; K k;
// k = p.key
// , key
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 {
// int binCount
//
for (int binCount = 0; ; ++binCount) {
// e = p.next
//
if ((e = p.next) == null) {
//
//
p.next = newNode(hash, key, value, null);
// ( )
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//
treeifyBin(tab, hash);
//
break;
}
// k = e.key key
// ,
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;
// !onlyIfAbsent true
if (!onlyIfAbsent || oldValue == null)
//
e.value = value;
//
afterNodeAccess(e);
// ;
return oldValue;
}
}
// , 1
++modCount;
// , 1
// hashmap
if (++size > threshold)
// hashMap
// hashmap
resize();
//
afterNodeInsertion(evict);
return null;
}
3関連する他の方法
// hashmap
resize();
//
newNode(hash, key, value, null);
//
((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//
afterNodeAccess(e);
//
treeifyBin(tab, hash);
//
afterNodeInsertion(evict);
3.1 resize()
/**
*
* , ,
* , ( ),index ( )
* 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;
//
// newCap
// newThr
int newCap, newThr = 0;
// 0
if (oldCap > 0) {
// MAXIMUM_CAPACITY hashmap 。1<<30。
// hashmap
if (oldCap >= MAXIMUM_CAPACITY) {
// Integer.MAX_VALUE = 2<<31-1
// hashmap
threshold = Integer.MAX_VALUE;
//
return oldTab;
}
// newCap = oldCap << 1 ;
// MAXIMUM_CAPACITY hashmap 。1<<30。
// DEFAULT_INITIAL_CAPACITY hashmap ,16
// hashmap , , 16
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//
newThr = oldThr << 1; // double threshold
}
// 0
else if (oldThr > 0) // initial capacity was placed in threshold
//
newCap = oldThr;
// 0
else { // zero initial threshold signifies using defaults
// 16;
newCap = DEFAULT_INITIAL_CAPACITY;
// ,0.75*16 = 12, , 12 ,
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 = 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) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode)e).split(this, newTab, j, oldCap);
else { // preserve order
Node loHead = null, loTail = null;
Node hiHead = null, hiTail = null;
Node next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
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;
}