从《浅析java8中HashMap的结构》这篇文章我们可以得知,hashMap的数据结构是数组+单链表。接下来咱们通过解读HashMap的put(K key, V value)方法来了解他的数据存储机制。
大致步骤如下图:
至于详细的步骤,请结合上图参考部分源码和注释:
static final int hash(Object key) {
int h;
//这里调用Object的native方法获取hashcode
//h>>>16 等价于 h/2^16
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
// i为数组下标 p为数组项中存放的链表
Node<K,V>[] tab; Node<K,V> p; int n, i;
//判断当前数组容器是否已初始化,如果没有则执行容器初始化操作
if ((tab = table) == null || (n = tab.length) == 0)
//初始化得到的数组长度为16(static final int DEFAULT_INITIAL_CAPACITY = 1 << 4)
n = (tab = resize()).length;
//根据hash值计算出将要存放目标值的数组下标,并判断该下标对应的位置是否已存在链表
if ((p = tab[i = (n - 1) & hash]) == null)
//如果不存在则在该位置创建一个新的链表。并将目标值存入新链表的表头
tab[i] = newNode(hash, key, value, null);
else {
//如果该位置存在链表(即hash值冲突了)
Node<K,V> e; K k;
//判断目标值的hash(key)是否和链表头节点的hash值相等,并且目标值的key和链表头节点存储的key是否相等(即:已存在目标key)
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//如果存在则将该节点取出
e = p;
//判断当前链表是否是树状结构(红黑树)
else if (p instanceof TreeNode)
//将目标key-value存入红黑树
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//执行到到这里已经确定了数组项中存放的结构为链表而非红黑树,
//遍历链表,直到链表尾部(尾节点的下一节点为null)
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
//找到尾节点,在后面创建新的节点,并将目标key-value存入该新节点
p.next = newNode(hash, key, value, null);
//判断链表长度是否达到了树的阈值(8-1)
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//如果单个链表长度达到阈值,则进行重新散列(即如果数组容器的size>=64则链表变换成红黑树,否则只是单纯的散列)
treeifyBin(tab, hash);
//结束本次循环
break;
}
//如果链表中存在有和目标key-value相同hash值,相同key的节点,则结束本次循环
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//如果链表当前节点与目标key-value的hash值和key都不相同,则将指针往后移动一位,指向链表的下一个子节点。准备下一轮遍历
p = e;
}
}
//如果e不为null,说明HashMap中存在key,这里执行修改value操作,并将旧值返回
if (e != null) { // existing mapping for key
V oldValue = e.value;
//onlyIfAbsent如果为true则表示不允许修改该节点的value值
if (!onlyIfAbsent || oldValue == null)
e.value = value;
//此方法是为了继承HashMap的LinkedHashMap类服务的
afterNodeAccess(e);
//修改值后,返回旧值
return oldValue;
}
}
//HashMap修改次数自加
++modCount;
//判断数组容器的size,确认是否需要将容器进行扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
//Hashmap中不存在key,所以返回null
return null;
}