hashmap深入分析

hashmap底层数据结构

hashmap-逻辑结构

PUT方法

hashmap-put方法

public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
//table全局变量,存储链表头节点数组
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果table数组是空的,则创建一个头结点数据,默认长度是16
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//n是table长度,根据数组长度和key的哈希值,定位当前key在table中的下标位置,如果为空则新建一个node。不为空走else
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
// 如果新的key与table中索引处取出的头节点的key相等,且hash值一致,则把新的node替换掉旧的node
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果头节点不是空,且头节点的类型是树节点类型,则把当前节点插入当前头节点所在的树中(红黑树,防止链表过长,1.8的优化)
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//map的数据结构处理
else {
//遍历链表
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//如果链表长度大于或等于8,则把链表转化为红黑树,重点转化方法(treeifyBin)
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//此方法构建红黑树
treeifyBin(tab, hash);
break;
}
//如果当前节点的key和hash均和待插入的节点相等,则退出循环,(注意此时e的值在前一个if时赋值过,因此当前的e值,就是链表中的当前节点值)
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;
//此方法实现的逻辑,是把入参的节点放置在链表的尾部,但在HashMap中是空实现,在LinkedHashMap中有具体实现
afterNodeAccess(e);
return oldValue;
}
}
//保证并发访问时,若HashMap内部结构发生变化,快速响应失败
++modCount;
//当table[]长度大于临界阈值,调用resize方法进行扩容
if (++size > threshold)
resize();
//此方法在HashMap中是空方法,在LinkedHashMap中有实现
afterNodeInsertion(evict);
return null;
}

hashmap的hash算法和寻址算法如何优化

hashmap如何解决hash碰撞

hashmap如何扩容

final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
//oldCap---原hashMap的最大容量,oldThr---原hashMap的负载容量
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;
}
//左移一位,就是将原来的容量翻倍,翻倍后的值小于2的30次方,大于原来的容量值
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
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;
}