public V put(K key, V value) {
//hash(key):根据hash算法算出key的哈希值
return putVal(hash(key), key, value, false, true);
}
static final int hash(Object key) {
int h;
// h >>> 16:无符号右移16位 ^ :异或
//异或:相同取0,不同取1
//(h = key.hashCode()) ^ (h >>> 16):获取h的高十六位
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//onlyIfAbsent:如果为true时,添加的值已存在时,则不添加进去
//evict:是否HashMap创建期间
//返回值,如果添加的值不存在,则返回null,如果存在,则返回老值
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
//数组的类型为节点类型的数据Node
Node<K,V>[] tab;
//节点指针,用来定位目标节点插入的位置
Node<K,V> p;
int n, i;
//数组为空时,初始化数组的长度为 resize()
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;
//判断目标节点与插入位置的节点是否为相同的类型
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//判断p是否实现了TreeNode
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);
//判断节点数是否超过成为数的阈值:TREEIFY_THRESHOLD=8
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//将链表转换为红黑树的结构,这里会先对数组的长度进行判断,如果小于64,则直接进行扩容
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;
//在LinkedHashMap会调用该方法,
afterNodeAccess(e);
return oldValue;
}
}
//修改数据次数+1
++modCount;
//数组size实际长度+1,判断实际长度是否大于阈值threshold,大于则进行调整容量
if (++size > threshold)
resize();
//在LinkedHashMap会调用该方法,
afterNodeInsertion(evict);
return null;
}
final Node<K,V>[] resize() {
//获取hashmap的数组信息
Node<K,V>[] oldTab = table;
//获取原先数组的容量
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//获取原先数组的容量阈值
int oldThr = threshold;
//定义新的容量、容量阈值
int newCap, newThr = 0;
//对原先数组的不同做不同的处理
//容量大于0时
if (oldCap > 0) {
//容器的最大容量不能超过,如果超过则使用容器的最大容量0x7fffffff
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//进行扩容:2倍扩容
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;
//原先容量为0时,进行初始化
//初始化扩容阈值和默认初始化容量:16,8
else { // zero initial threshold signifies using defaults
//默认容量:16
newCap = DEFAULT_INITIAL_CAPACITY;
//初始化阈值:容量*因子:16*0.75=8
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);
}
//更新阈值
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;
//e = oldTab[j]:e节点指向数组,即链表头结点或者树的根节点
if ((e = oldTab[j]) != null) {
//清空元素,方便回收老的数组
oldTab[j] = null;
//只有一个元素
if (e.next == null)
//e.hash & (newCap - 1)重新hash计算,分布到新的位置
newTab[e.hash & (newCap - 1)] = e;
//节点属于树节点
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//节点属于链表
else { // preserve order
//hash值为0的头结点,hash值为0,不管数组容量多少,最终与运算出的结果都为0
Node<K,V> loHead = null, loTail = null;
//hash值为0的头结点
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
//循环遍历链表,将原先链表进行重新分布到新的数组中
do {//jdk1.8使用的是尾插法
next = e.next;
//hash值为0
if ((e.hash & oldCap) == 0) {
if (loTail == null)
//指定头结点
loHead = e;
else
loTail.next = e;
loTail = e;
}//hash值不为0
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
//hash计算为0,添加的位置都在数组的索引值为0
if (loTail != null) {
//尾结点指向null
loTail.next = null;
//数组位置放置头结点,因为通过之前的循环已经练成一条完整的链表了
newTab[j] = loHead;
}
//hash值不为0,需要重新进行哈希计算位置
if (hiTail != null) {
//尾结点指向null
hiTail.next = null;
//数组位置放置头结点,因为通过之前的循环已经练成一条完整的链表了
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}