Java HashMap 的 get 和 put 方法的实现流程
put 方法流程
put 方法的核心流程如下:
1.计算 key 的 hash 值
hash 计算方法:
static final int hash(Object key) {int h;return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
h >>> 16
:int 类型是 32 位,移动 16 位刚好是一半,将 h 右移 16 位,高 16 位补 0,相当于把原始哈希值的高 16 位移到低 16 位h ^ (...)
:将原始哈希值与右移 16 位后的值进行异或运算- 这个操作的效果是让原始哈希值的高 16 位和低 16 位都参与到最终哈希值的计算中
通过高 16 位与低 16 位异或,减少哈希冲突
2.检查 table 数组是否为空或长度为 0,如果是则进行 resize () 初始化
resize 方法:当 table 为空时进行初始化,或 size 超过 threshold 时进行扩容,扩容后容量翻倍,并重新分配所有元素
3.根据 hash 值找到对应的桶位置 (i = (n - 1) & hash)
4.如果桶为空,直接插入新节点
5.如果桶不为空,检查第一个节点是否匹配
6.如果第一个节点不匹配,检查是否为树节点
7.如果是树节点,调用树的插入方法
8.如果不是树节点,遍历链表
9.遍历过程中如果找到相同 key 的节点,替换其 value
10.如果遍历到链表尾部仍未找到,插入新节点
11.插入后检查链表长度是否达到 8,如果是则将链表转换为红黑树
红黑树转换:
当链表长度达到 8 且 table 长度达到 64 时,链表转换为红黑树;当红黑树节点数小于 6 时,转换回链表
12.插入成功后,size 加 1,检查是否需要扩容
源码实现:
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;// 步骤2:检查table是否为空或长度为0,是则resize初始化if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;// 步骤3:根据hash找到桶位置,步骤4:如果桶为空,直接插入新节点if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node<K,V> e; K k;// 步骤5:检查第一个节点是否匹配if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;// 步骤6、7:检查是否为树节点,是则调用树的插入方法else if (p instanceof TreeNode)e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);else {// 步骤8:遍历链表for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) {// 步骤10:插入新节点到链表尾部p.next = newNode(hash, key, value, null);// 步骤11:检查链表长度是否达到8,是则转为红黑树if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}// 步骤9:遍历过程中找到相同key的节点if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}// 步骤9的后续:替换已存在节点的valueif (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}// 步骤12:插入成功后操作++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;
}
get 方法的核心流程如下:
- 计算 key 的 hash 值
- 检查 table 数组是否为空且对应的桶是否为空
- 如果桶的第一个节点匹配,直接返回
- 如果不匹配,检查是否为树节点
- 如果是树节点,调用树的查找方法
- 如果不是树节点,遍历链表查找
源码实现:
public V get(Object key) {Node<K,V> e;return (e = getNode(hash(key), key)) == null ? null : e.value;
}final Node<K,V> getNode(int hash, Object key) {Node<K,V>[] tab; Node<K,V> first, e; int n; K k;// 步骤2:检查table是否为空且对应的桶是否为空if ((tab = table) != null && (n = tab.length) > 0 &&(first = tab[(n - 1) & hash]) != null) {// 步骤3:检查第一个节点是否匹配if (first.hash == hash && // always check first node((k = first.key) == key || (key != null && key.equals(k))))return first;// 步骤4、5:检查是否为树节点,是则调用树的查找方法if ((e = first.next) != null) {if (first instanceof TreeNode)return ((TreeNode<K,V>)first).getTreeNode(hash, key);// 步骤6:遍历链表查找do {if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))return e;} while ((e = e.next) != null);}}return null;
}