JDK8的源码是Node数组+Node链表+TreeNode组成
常量解释
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 //Node数组默认长度
static final int MAXIMUM_CAPACITY = 1 << 30; //Node数组最大长度
static final float DEFAULT_LOAD_FACTOR = 0.75f; //扩容因子
static final int TREEIFY_THRESHOLD = 8; //有一个Node链表长度大于8 则有可能转化为TreeNode
static final int UNTREEIFY_THRESHOLD = 6; //在Hash表扩容时候,如果发现当前链表长度小于6了则重新退化为Node链表
//在转为TreeNode之前还会有一次会比较Node数组的长度,如果<64则Hash表仅是扩容,这样做的目的是,如果多个K/V恰好在同一个Node链表上导致的不必要的转换
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
初始化
HashMap有三个初始化方法
public HashMap(int initialCapacity, float loadFactor) {
//手动设置容量<0抛出异常
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
//手动设置容量超过最大容量,为最大容量
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
//扩展因子<=0 或者 不合法 抛出异常
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
//计算Hash表的扩容阀值
this.threshold = tableSizeFor(initialCapacity);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
/**
* Returns a power of two size for the given target capacity.
* 翻译:返回大于输入参数且最近的2的整数次幂的数,例如9 返回16 ,17返回32 18返回32
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
put数据
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;
}
}
//修改次数+1
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
Node链表转为Tree
treeifyBin (Node转为TreeNode)
//容量数组为空或者容量数组长度 < 64 继续扩容,避免不必要的转换
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
扩容
//容量扩容一倍,扩容阀值也是一倍
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;
else { // zero initial threshold signifies using defaults
//首次初始化,容量是默认值,扩容阀值是容量*扩容因子
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
//将老的Node数组里的Node重新放到扩容之后的Node数组,分为3中情况,当前Node next为空,TreeNode节点,当前Node next不为空
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
//对于非0的key,其在新数组中的位置是需要更新的,需要存储在新增的数组中的一个新的位置,将其形成一个链条。
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;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
扩容避免1.7的出现的环形队列
JDK1.7高并发中出现环形队列的代码
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
//以下代码容易出现环形队列
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
1.8解决这个问题
1、首先改头插法为尾插法
2、使用高位和低位Node节点来放不同位置的Node
线程不安全
putVal()
resize()