HashMap put方法
// HashMap put方法
public V put(K key, V value) {
// hash(key) 根据key找到具体的位置
return putVal(hash(key), key, value, false, true);
}
// hash 的算法
static final int hash(Object key) {
int h;
// 如果key是null,就直接放到第一位,否则就计算出具体的坐标
// h = key.hashCode() 为第一步 取hashCode值
// h ^ (h >>> 16) 为第二步 高位参与运算
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
// evict: if false, the table is in creation mode.
// onlyIfAbsent: if true, don't change existing value
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab;
Node<K,V> p;
int n, i;
// 第一次往里面hashMap里面插入值
// 往里面put值,如果table为空,则初始化resize()方法
if ((tab = table) == null || (n = tab.length) == 0)
// 将初始化的table长度记录下来,给n
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;
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);
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;
}
}
// 只要hashMap的结构发生变化就size 增加一下次数
++modCount;
// size 是当前hashMap中的元素数目
if (++size > threshold)
resize();
// if false, the table is in creation mode.
// hashMap的put方法,evict默认是true,即非创建模式
afterNodeInsertion(evict);
return null;
}
// 可以看出,扩容后的hashMap结构是数组
final Node<K,V>[] resize() {
// 先将要扩展的表做一个备份
Node<K,V>[] oldTab = table;
// 记录即将要扩展的表的长度(容量)
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 记录即将要扩展的表的阈值
int oldThr = threshold;
// 初始化新的hashMap容量及阈值
int newCap, newThr = 0;
// 旧表容量大于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
}
// 旧表阈值大于0
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
// 这个说明旧表的阈值、长度都是小于等于0的,就直接使用默认值
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
// 如果新表阈值等于0,就重新根据负载因子计算阈值
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
// 将新的阈值给到当前hashMap的阈值
threshold = newThr;
// 初始化新的Node表——数组
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
// 初始化好直接给到当前hashMap的table
table = newTab;
// 旧表不为空就开始真正的resize操作
if (oldTab != null) {
// 以旧表的容量为索引,开始复制
for (int j = 0; j < oldCap; ++j) {
// 每次循环从旧表中拿到的Node值给到e
Node<K,V> e;
if ((e = oldTab[j]) != null) {
// e已经有值了,旧表上的j位置就没有用了,直接释放掉
oldTab[j] = null;
// 如果e节点下一个为空,说明已经复制完,将当前不为空的e给到新表对应的位置即可,这个resize结束
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
// 如果e节点下一个不为空,会进到这里,判断是否是TreeNode节点,如果是,则开始扩容。红黑树
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//红黑树
else { // preserve order
// 之前的链表,重新resize的操作(需要重新计算hash值)
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;
}
HashMap get方法
public V get(Object key) {
Node<K,V> e;
// 根据key找到对应的节点,把节点的值返回回去即可
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab;
Node<K,V> first, e;
int n;
K k;
if (
(tab = table) != null // 首先保证表不能为空
&& (n = tab.length) > 0 //表里面要有数据
&&
(first = tab[(n - 1) & hash]) != null // 拿到第一个节点且第一个节点不能为空
) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
// 如果是红黑树,则从红黑树中查找值
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
// 不是红黑树则依次按照顺序往下面找,直到找到目标节点返回,找不到就退出do-while循环
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
// 都找不到,就返回null
return null;
}