最近看了一下源码,怕忘了,来总结一下
底层数据结构
哈希值,键值对,下一个结点
final int hash;
final K key;
V value;
Node<K,V> next;
扰动函数,它的目的是让key的Hash值高16位参与路由运算,重新Hash后得到的结果与高16位也有关系,从而减少碰撞。
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
put操作
套娃了putVal
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
putVal
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//第一次put,创建散列表
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//路由运算后得到的桶位是null,new一个Node放进去
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;
}
}
//如果数组长度达到扩容阈值,进行扩容
++modCount;
if (++size > threshold)
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) {
//数组大小达到最大值的情况,把扩容条件设为int最大值,不扩容了,返回旧表,很少用到
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//如果当前大小左移一位后小于最大值,并且大于等于初始值,即16,让扩容阈值等于当前阈值左移一位的结果
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;
//创建一个长度为16的散列表
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);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
//如果扩容之前table不为null,遍历散列表,创建一个临时遍历Node
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
//如果当前桶位有数据,将他赋给临时变量
if ((e = oldTab[j]) != null) {
//赋空,为了方便JVM GC时回收内存
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;
//将hash与旧表容量相与,为何要这么做呢
//假如当前数组容量为16,那么存放在最后一位的元素的hash值最后五位只有0 1111和1 1111两种情况,将它与数组容量16即1 0000相与,则很容易知道0 1111的情况结果为0,这样就方便了计算
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;
}