//我们调用的put方法
public V put(K key, V value) {
//真正的put方法
return putVal(hash(key), key, value, false, true);
}
/*
Implements Map.put and related methods.
Params:
hash – hash for key
key – the key
value – the value to put
onlyIfAbsent – if true, don't change existing value
evict – if false, the table is in creation mode.
Returns:
previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//table为我们实际保存数据节点数组,长度为0或2的倍数(原因:计算新旧索引值时,可以保证只有最高位不同)
//当table为空,说明目前的map没有被使用
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; K k;
//如果该索引位置有值,且hash值与key值均与待插入值相等
//在onlyabsent为false且原值为空得情况下,更新该值。
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果p为红黑树,则按照红黑树的加入方法加入该值
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//否则,说明e为链表结构,遍历分情况插入
for (int binCount = 0; ; ++binCount) {
//当下一个为null时,将新值插入到e的最后。
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//图个此时bincount(即为链表的长度)大于平衡树阈值(6),则需要将链表调整为红黑树结构
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//找到hash值相同的元素时,直接退出
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//否则,向后遍历
p = e;
}
}
//当e!=NULL时,则说明原有的位置已经存在值
if (e != null) { // existing mapping for key
V oldValue = e.value;
//在onlyabsent为false且原值为空得情况下,更新该值。
if (!onlyIfAbsent || oldValue == null)
e.value = value;
//否则调用LinkedHashMap的afterNodeAccess方法插入新值
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//当加入新值后,map长度大于阈值时,扩容
if (++size > threshold)
resize();
//无法扩容时,则移除最早加入hashmap的对象。
afterNodeInsertion(evict);
return null;
}
/**
* 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<K,V>[] resize() {
//先存一份扩容前的数据
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
//当扩容前容量大于0,即已经被初始化或被扩容过时
if (oldCap >= MAXIMUM_CAPACITY) {
//如果已经达到最大存储容量,则无法扩容
threshold = Integer.MAX_VALUE;
return oldTab;
}
//否则扩容为原来的两倍
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;
else {
// zero initial threshold signifies using defaults
//否则进行初始化
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//当新的阈值为0时,按照负载因子*大小的公式来计算新的阈值
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数组
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
//更新table
table = newTab;
//当oldtab!=NULL时,进行复制
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
//置空oldTab 节省空间?
oldTab[j] = null;
if (e.next == null)
//当e下再也没有别的元素时,重新hash
newTab[e.hash & (newCap - 1)] = e;
//如果e是树结构
else if (e instanceof TreeNode)
//对e进行拆分
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { //前序遍历e,将e中的元素重新放入新的tab中 // preserve order
//xxhead指向链表头部,xxtail指向链表尾部
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
//循环,直到e.next为空。
do {
//hashmap的高低位索引机制
//原hash值在旧容量的最高位为0,则加入低位(原索引位置)
//若为1 则加入高位(原索引位置+原容量)
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);
//放入新的tab中
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
HashMap 扩容方法,put方法源码解读
最新推荐文章于 2024-07-09 16:42:24 发布