hash(Object key)
int类型4个字节,32位,将h右移动16位。再异或运算。即 高16位与低16位进行异或,每一位都充分参与运算,这样得到的值更加散列。
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
putVal()方法
下列代码行4、5:
刚进来时table为null,调用resize()方法中返回newTab,则n为16。
DEFAULT_INITIAL_CAPACITY=16
newCap = DEFAULT_INITIAL_CAPACITY;
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
代码行6:&运算,即取%。且效率比%高。
用于判断当前tab[i]是否为null
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)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);//创建节点,引出HashMap是一个数组+链表的结构
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)
treeifyBin(tab, hash);//当到达阈值8时,链表转化成红黑树
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)//onlyIfAbsent:是否替换旧值,默认替换
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)//threshold=12*0.75=12
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) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && //newCap=2*16
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // 对应阈值也发生变化=2*12
}
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) {//一些边界值的判断
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;
}
上图代码中:(e.hash & oldCap) == 0 用于判断当前节点是否需要重新被放置到新扩容的部分
假设之前有节点 hash1=6,hash2=22,oldCap=16
未扩容前,处于同一个下标
结果为0,说明还是在原来的部分,否则放置 newTab[j + oldCap] = hiHead;