引入
在理解hashmap的put方法之前,需要对hashmap有一些基础的理解。
1.hashmap的数据结构:数组 + 链表 + 红黑树
2.put数据的大概流程:
1.首次put数据
当定义了一个hashmap结构,在第一次调用put方法进行,添加数据的时候,首先会对hashmap结构进行初始化,生成长度为16的数组。
然后插入的数据调用hash(key)生成hash值,用来定位数据在数组中的位置,若之后再次插入数据的位置与之前相同则使用链表的结构,继续向下插入,直到链表长度等于8,将链表转化为红黑树。
1.定义的常量
//数组的默认大小。
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//数组的最大值
static final int MAXIMUM_CAPACITY = 1 << 30;
//数组的扩容因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//链表的上限个数
static final int TREEIFY_THRESHOLD = 8;
//红黑树节点个数小于6转化为链表
static final int UNTREEIFY_THRESHOLD = 6;
//转换红黑树的最小节点数,小于64不会转化红黑树,而是选择扩容
static final int MIN_TREEIFY_CAPACITY = 64;
Node节点
static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //每个节点的hash值,长度32位
final K key;
V value;
Node<K,V> next; //链表结构的下一个节点
...
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
...
}
变量
//表示整个hashmap数据结构
transient Node<K,V>[] table;
//记录数组使用格子的情况
transient int size;
//当前数组容量*扩容因子:表示扩容元素个数临界值
int threshold;
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
计算hash值的方法,返回长度为32位的hash值
static final int hash(Object key) {
int h; //int 32位
//将key的hashcode值的高16位与低16位进行异或运算,为了不同的key的hash值尽可能不同
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
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;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//以上三行代码是put方法的第一步,如果是第一次put那么进行初始化,tab数组代表hashmap结构,n为数组的长度。
//put数组有三种情况,1:插入的位置为空,2:插入的位置为链表,3:插入的位置为树节点
//1.如果插入的位置为空,new一个节点,将数据存到此位置,这一步还将此位置的数据复制给了p
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);
//如果要插入的位置的节点为链表的处理
//考虑链表的长度,如果大于8要将链表转化为红黑树
else {
for (int binCount = 0; ; ++binCount) {
//如果当前位置的下一个节点为空
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;
}
//进入循环时,就将p.next的值赋给了e,如果key值相同,直接跳出循环,判断e值进行插入即可
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//
p = e;
}
}
//如果e值不为空,代表时相同的key值进行的value值替换,此时返回被替换掉的值
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//size为当前节点的个数,threshold为扩容的阈值
//如果当前的节点数大于阈值将调用resize()扩容(每次以两倍数组大小进行扩容)
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; //存储总容量
int 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; // 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; //16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); //0.75*16=12
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
//threshold
threshold = newThr;
//第一次put创建初始化的数组,没次扩容形成的新的数组
@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;
//如果下面时红黑树,用红黑树的split方法进行处理
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;
//这里没有像前面和(n-1)做'与操作'
//当前数据的位置的hash值位置的前一位如果是0,则满足(e.hash & oldCap) == 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);
//为0在原来位置不动
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
//不为0在原来位置加上原容量的位置
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}