hashmap底层就是一个数组,为了存储更多的数据他会在数组的桶位
1.7 数组+ 列表
1.8+数组+列表+红黑树
hashmap关键点
- 懒加载思想,第一次put时resize才会创建Node
- 右移16位,高16位和低16位^(异或,1 %50,0 %50),更分散
- 大于8,再判断size大于64才会列表转红黑树
存放寻址从put方法开始,关键
put时先判断table是否为空,空的话会用扩容,默认16容量
不为空的话,计算数组下标
计算方式hashcode(&)与上length-1
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);
else {