HashMap底层代码解析,在高并发下,会报ConcurrentModificationException:
put方法:
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
hash方法,使用高16位异或低16位主要是从速度,功效和质量来考虑的,减少系统的开销,也不会造成因为高位没有参与下标的计算,从而引起的碰撞:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
putVal方法:
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)
//第一个put进来,会执行resize()扩容方法。
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
//判断当前数组的(n - 1) & hash下标是否为null,如果null值的话,就newNode存放在tab[i]下标位置上。
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))))
//如果keyhash值相等,且值相等,则节点替换赋值,这里就是为什么hashMap中是不存在重复的节点。
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);
/**
*
如果链表长度达到8,就会开始红黑树化,其实链表长度达到8不一定开始树化了,treeifyBin中还有个条件是tab.length>64,就是数组的长度>64,满足两个条件才会开始树化,这是为了减少树化,因为黑红树是中自平衡的二叉查找树,插入到一定条件下会旋转,旋转的过程是会消耗性能的
**/
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+1,这个值主要用来计map的修改次数
++modCount;
//当第一次Map容量>12时,就会进行扩容,最终也是会调用resize方法
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
resize()
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) {
if (oldCap >= MAXIMUM_CAPACITY) {
//当当前数组长度》=MAXIMUM_CAPACITY(1073741824)
threshold = Integer.MAX_VALUE;//阈值就会设置成最大Int值2^32-1
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold 阈值X2,双倍
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
//put第一个参数进来,会先初始话map容量,默认16,扩容数threshold=12,DEFAULT_LOAD_FACTOR=0.75
newCap = DEFAULT_INITIAL_CAPACITY;//16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//12
}
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) {//如果当前元素不为null,需要重新hash计算
oldTab[j] = null;//将原来该位置的元素设置null
if (e.next == null)//如果该位置的元素只有一个元素,不存在链表,则可以直接hash计算赋值
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;//next不为null
if ((e.hash & oldCap) == 0) {//如果两者&出来的值=0,就会重新建链,然后将建好的链存在在原来的位置
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {//如果两者&出来的值!=0,就会重新建链,会存放在数据下标为当前位置+oldCap的位置上
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;
}
split()
final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
TreeNode<K,V> b = this;
// Relink into lo and hi lists, preserving order
TreeNode<K,V> loHead = null, loTail = null;
TreeNode<K,V> hiHead = null, hiTail = null;
int lc = 0, hc = 0;
//下面的流程和链表的拆分操作有相似之处
for (TreeNode<K,V> e = b, next; e != null; e = next) {
next = (TreeNode<K,V>)e.next;
e.next = null;
if ((e.hash & bit) == 0) {
if ((e.prev = loTail) == null)
loHead = e;
else
loTail.next = e;
loTail = e;
++lc;
}
else {
if ((e.prev = hiTail) == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
++hc;
}
}
if (loHead != null) {
//当长度<=6就会开始解除树化
if (lc <= UNTREEIFY_THRESHOLD)
tab[index] = loHead.untreeify(map);
else {
tab[index] = loHead;
if (hiHead != null) // (else is already treeified)
//重新树化
loHead.treeify(tab);
}
}
if (hiHead != null) {
//当长度<=6就会开始解除树化
if (hc <= UNTREEIFY_THRESHOLD)
tab[index + bit] = hiHead.untreeify(map);
else {
tab[index + bit] = hiHead;
if (loHead != null)
//重新树化
hiHead.treeify(tab);
}
}
}
treeifyBin()
/**
* Replaces all linked nodes in bin at index for given hash unless
* table is too small, in which case resizes instead.
*/
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
//Node节点转成TreeNode节点
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)//开始形成红黑树
hd.treeify(tab);
}
}
treeify(),可以深度了解一下红黑树
/**
* Forms tree of the nodes linked from this node.
*/
final void treeify(Node<K,V>[] tab) {
TreeNode<K,V> root = null;
for (TreeNode<K,V> x = this, next; x != null; x = next) {
next = (TreeNode<K,V>)x.next;
x.left = x.right = null;
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
else {
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
//树自平衡,重新染色
root = balanceInsertion(root, x);
break;
}
}
}
}
moveRootToFront(tab, root);
}
HashMap的插入大致先了解到这里。
这里再插一句就是为什么会导致链表循环:循环链表是发生在扩容的时候形成的参考一下https://blog.csdn.net/qq_36520235/article/details/86653136