首先是添加元素的put()方法:
//hashMap的添加元素方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//hash()方法,用于计算当前元素的hash值
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
下面是putVal()方法:
putVal()的执行流程:
1.put方法传入键值对
2.计算hash值
3.根据hash值找到在数组中的存放位置
如果无哈希冲突,则直接存入数组中
如果出现了哈希冲突时:
如果当前的数据结构是红黑树 则存入到红黑树
如果当前的数据结构是链表时:
如果链表插入之后长度小于8,则直接插入到链表中
如果链表插入之后长度大于等于8,则将链表转化为红黑树,存入红黑树中
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果数组为空,调用resize()方法进行初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//i = (n - 1) & hash获取数组的索引位置,如果计算的索引对应的位置上没有元素,则直接插入该节点
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//出现了哈希冲突,通过链表或红黑树解决冲突
else {
Node<K,V> e; K k;
//如果该索引对应的已经存在的元素与待插入的元素key相等,则覆盖
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果p已经存在,而且是红黑树
else if (p instanceof TreeNode)
//那么将该元素添加到红黑树中
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//如果p已经存在,不是红黑树而是链表
else {
//遍历链表
for (int binCount = 0; ; ++binCount) {
//如果节点链表的next为空
if ((e = p.next) == null) {
//将待添加的节点直接添加到链表中
p.next = newNode(hash, key, value, null);
//如果节点链表的数量超过8个,则将其转化为红黑树,以便于查找
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果待传入的key和链表中已经存在的key相同,则直接退出,因为HashMap中无重复元素
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;
//现有数组的容量等于数组长度,数组为空时,容量为0
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
//先有容量大于0,则已经扩容过了
if (oldCap > 0) {
//现有容量大于最大容量,直接返回
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 ,但是当前扩容阈值 > 0,调用有参构造函数会到这里
else if (oldThr > 0)
newCap = oldThr;
else {
// 新的容量等于默认容量
newCap = DEFAULT_INITIAL_CAPACITY;
// 新的扩容阈值等于默认负载因子0.75*默认容量16=12
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;
}