一、前言
HashMap太太太常用了,不做过多的介绍。进入正文直接冲源码。 本文主要是基于1.8的HashMap讲述部分源码,主要重点是put()方法和resize()方法
二、 HashMap 的构造方法
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
}
三、 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) {
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 {
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)
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) {
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) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
} else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1;
} else if (oldThr > 0)
newCap = oldThr;
else {
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;
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 {
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;
}
(二)、流程图
(三)、 resize方法的注意事项
resize()方法有2个注意点
一 、 就是newCap, newThr新的容量和新的扩容阈值的赋值这段if else逻辑判断,分别对应了三种情况,在上述流程图中有标注
(1)第一种情况:原来map中已经有元素,属于真实扩容 (2)第二种情况:第一次添加元素,且用的是有参构造出来的HashMap (3)第三种情况:第一次添加元素,且用的是无参构造出来的HashMap
二、移动链表里的元素的时候,以为链表中存在的元素可能较多,需要将其移动
举个例子 假如有元素3 其hashcode计算得出是0000 0011 以及元素11其hashcode是0001 0011 那按照put方法,计算其位置 0000 0011 & 0000 1111 = 0000 0011 0001 0011 & 0000 1111 = 0000 0011 则他们2个都是放在3这个位置,注意这个时候与的是(16-1 )=15 新计算的位置的时候 与的是16 0000 0011 & 0001 0000 = 0000 0000 0001 0011 & 0001 0000 = 0001 0000 根据计算结果是否为0,判断其放在原来的位置还是需要移动 如果产生移动,将会产生2个链表,一个低位链表,一个高位链表;故而有四个变量 Node<K, V> loHead = null, loTail = null; Node<K, V> hiHead = null, hiTail = null;
备注:HashMap中这两个方法算是逻辑比较复杂的,理解这2个方法以后,get方法和remove方法基本也能理解。至于红黑树的相关源码,后续再写一篇相关的总结。
如果这篇【文章】有帮助到你,希望可以给博主点个赞👍,创作不易。后面我还会继续更新Redis集群的搭建、Redis的面试题等文章,我们一起继续学习吧。
|