HashMap
节点:
filed:

table总会是2的幂次大小,并且允许为0。
threshold控制resize的阀值。

上座率:如其名
构造方法:

检查了容量上下限,负载因子是否是Float.NaN.
看一下这个容量算法,返回大于输入参数且最近的2的整数次幂的数。比如10,则返回16。
该算法让最高位的1后面的位全变为1。
最后再让结果n+1,即得到了2的整数次幂的值了。

带一个参数的构造方法,默认负载因子是0.75


不带参数的构造方法:

不带参数的构造方法:
只设置了loadFactor
看一下putMapEntries:
看注释可以知道,map的putall和上面的构造方法会用到,且在构造方法中使用时evict为false。

如果当前table为空 以当前容量的4/3初始化阀值。检查put的map大小是否超过阀值,超过的话进行resize()(如果这里没超过,但原来的加新加的超过了呢??不resize吗?)。
看下reSize 逻辑特别/* * Initializes or doubles table size. If null, allocates in * accord with initial capacity target held in field threshold.
* Otherwise, because we are using power-of-two expansion, the
* elements from each bin must either stay at same index, or move
* with a power of two offset in the new table.
*
* @return the table
*/
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;//get老的table size
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
//处理oldCap边界
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//如果旧容量*2的值没有超过上限且旧容量不小于默认值16,将新阀值扩容至原来的2倍
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
//未初始化的容器 新容量等于阀值,什么情况下会发生
newCap = oldThr;
else { // zero initial threshold signifies using defaults
//未初始化且阀值为空的,默认容量16,阀值为16*0.75=12,什么情况下会发生
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
//兜底newThr为新容量的3/4
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)
//链表未位:旧的hash与新容量-1相与得到新位置
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
//如果是treeMap
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
//otherMap
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
//hash值与之前相与=0的一定是小于原来容量的 比如一个size=16的map ,10000,那么hash值为xx0xxxx一定还是落在原来的槽里,而xx1xxxx在新数组的位置应该是【j+oldCap】
//因为xx0xxxx右移4位得到的永远是16的偶数倍,而xx1xxxx右移4位得到的是16的奇数倍
//这里没有重新用新的Cap去hash是处于什么考量
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;
}
看完reSize()之后再看下一下hashmap是如何put新的元素的:

x
取“key”的hashcode的高16位???? 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)
//通过reSize去完成初始化
n = (tab = resize()).length;
//表长度-1与hash相与得到位置
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))))
//先检查链表第一个位置如果hash相等 key相等
e = p;
else if (p instanceof TreeNode)
//treeMap
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
//到达冲突链未位,新实例话一个node加到尾部,并检查当前的位置是否在第八个和之后,是的话构建红黑树
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//检查key是否相同
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;
}
}
++modCount;
if (++size > threshold)
//如果超过阀值 则resize
resize();
afterNodeInsertion(evict);
return null;
}
重写equals:
-
自反性(reflexive)。对于任意不为
null的引用值x,x.equals(x)一定是true。 -
对称性(symmetric)。对于任意不为
null的引用值x和y,当且仅当x.equals(y)是true时,y.equals(x)也是true。 -
传递性(transitive)。对于任意不为
null的引用值x、y和z,如果x.equals(y)是true,同时y.equals(z)是true,那么x.equals(z)一定是true。 -
一致性(consistent)。对于任意不为
null的引用值x和y,如果用于equals比较的对象信息没有被修改的话,多次调用时x.equals(y)要么一致地返回true要么一致地返回false。 -
对于任意不为
null的引用值x,x.equals(null)返回false。
对于Object类来说,equals()方法在对象上实现的是差别可能性最大的等价关系,即,对于任意非null的引用值x和y,当且仅当x和y引用的是同一个对象,该方法才会返回true。
需要注意的是当equals()方法被override时,hashCode()也要被override。按照一般hashCode()方法的实现来说,相等的对象,它们的hash code一定相等。
总结:
- 默认初始大小为16;
- 负载因子0.75;
- size总是2的幂次;
- null放在0槽;
- hash值为hashcode与高16为相与;
- 位置为hash值与table大小-1相与;
- 冲突链表长度>=8且table的大小大于等于64,冲突链会转换为红黑树,冲突链表长度>=8但table的大小 小于64会先进行resize;
- 扩容的新大小和新阀值是原来的两倍;
- 扩容算法非别用高位链表和地位链表完成,hash值与原size相与=0在地位表,否则在高位表(j+oldCap)

浙公网安备 33010602011771号