//初始化容量 16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
//负载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//链表长度达到8成,变成红黑树
static final int TREEIFY_THRESHOLD = 8
//节点数组
transient Node<K,V>[] table;
//默认无参构造
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
//指定初始容量
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
Node<K,V>[] tab;
Node<K,V> p;
int n, i;
//如果node数组等于null或者长度位0,则进行初始化扩容
if ((tab = table) == null || (n = tab.length) == 0)
//扩容代码 初始容量16
n = (tab = resize()).length;
//如果哈希后,对应数组的node节点为null,则直接赋值
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//如果健的值以及hash等于链表中的第一个键值对节点时,则将e指向该键值对
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;
}
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;
}
final Node<K,V>[] resize() {
//将node数组赋值给oldTab
Node<K,V>[] oldTab = table;
//三元运算,oldTab等于null的话,oldCap=0,否则等于oldTab.length
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//扩容的阈值
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
//table容量最大值,不在扩容
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
//2倍扩容
} else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY){
// double threshold
newThr = oldThr << 1;
}
}else if (oldThr > 0){
// initial capacity was placed in threshold
newCap = oldThr;
} else {
//如果oldCap等于0, 则初始容量位16
newCap = DEFAULT_INITIAL_CAPACITY;
//阈值位16*0.75 = 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);
}
//扩容后需要rehash,再分配
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;
}
https://segmentfault.com/a/1190000012926722