/** * */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;//16 默认初始化容量为16 static final int MAXIMUM_CAPACITY = 1 << 30;//最大容量 static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认负载因子 static final int TREEIFY_THRESHOLD = 8;//树化阀值 static final int UNTREEIFY_THRESHOLD = 6;//非树化阀值 static final int MIN_TREEIFY_CAPACITY = 64;//最小树化容量
transient Node<K,V>[] table;//存储元素数组 transient Set<Map.Entry<K,V>> entrySet; transient int size;//元素个数 transient int modCount;//修改次数 int threshold;//阀值 final float loadFactor;//负载因子 /** *Node内部类,元素key-value */ static class Node<K,V> implements Map.Entry<K,V> { final int hash; 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;
}
}
//求hash值 大于2^16 65536的hash值有所改变
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//默认构造函数
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // 负载因子默认0.75
}
//put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//put方法调用:hash 为key的hash值,onlyIfAbsent为fasle,evict为false
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//存储元素数组为空并且长度为0
if ((tab = table) == null || (n = tab.length) == 0)
//进行扩容 tab为扩容后的数组 n为扩容后的长度
n = (tab = resize()).length;
//m%n和m&n 当n满足2^k,是相等的
//p为table存在的同一索引的元素
//p为空,进行新增
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//p不为空,进行链表添加
Node<K,V> e; K k;
//p的hash值等于put的hash值 并且p的key值等于put的key值或者key不为空并put的key值等于p的key值
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//p赋值给e
e = p;
//p为TreeNode实例 红黑树节点
else if (p instanceof TreeNode)
//p putTreeVal为TreeNode 返回
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
// p其他
//单链表循环处理
for (int binCount = 0; ; ++binCount) {
//单链表只有一个头节点,直接新建一个节点
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//当大于等于树化阀值8,将单链表转红黑树
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)
//扩容2倍,重新调整数组
resize();
afterNodeInsertion(evict);
return null;
}
//putVal方法调起:当存储元素数组为空并且长度为0
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) {//数组长度达到最大值,则不变
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold 扩容2倍
}
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);
}
if (newThr == 0) {//临界值还为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;
}