public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
// 判断数组是否初始化
if ((tab = table) == null || (n = tab.length) == 0)
// 没有初始化 调用resize方法初始化
n = (tab = resize()).length;
// 通过计算hash值判断在数组下标是否有值
if ((p = tab[i = (n - 1) & hash]) == null)
// 没有值直接放在该数组下标位置
tab[i] = newNode(hash, key, value, null);
else {
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);
// 如果当前长度大于7 转为红黑树
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就是修改操作存放原数据的变量
if (e != null) { // existing mapping for key
// 取出旧的值
V oldValue = e.value;
// 一定会执行,onlyIfAbsent传进来的是false
if (!onlyIfAbsent || oldValue == null)
// 将新值赋值当前节点
e.value = value;
afterNodeAccess(e);
// 返回旧的值
return oldValue;
}
}
// 计数器。计算当前节点的修改次数
++modCount;
// 当前数组中的数据长度时候大于阈值
if (++size > threshold)
// 扩容
resize();
// 空方法
afterNodeInsertion(evict);
return null;
}
下面是扩容的源码解读
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
// 如果当前数组未null,把旧数组的容量设置为0
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 旧的扩容阈值
int oldThr = threshold;
int newCap, newThr = 0;
// 判断数组容量是否大于0 如果大于0 说明数组已经初始化
if (oldCap > 0) {
// 判断当前数组长度是否大于最大数组长度 MAXIMUM_CAPACITY = 1 << 30 = 1073741824
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
}
// 如果大于0 数组已经初始化
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);
}
// 初始化容量小于16时,扩容阈值没有赋值
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<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> 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<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> 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;
}