🏐 HashMap
2022年6月9日
- Java
🏐 HashMap
1. 类注释
- 概述
HashMap
实现了Map
的所有方法- 允许有 1个
null
的 key & 多个null
的 value HashMap
和HashTable
类似,区别在于HashMap
非线程安全 & 允许null
- 复杂度
- O(1):
get
put
- O(数组长度 + KV 对的大小):
Iteration
- 2个重要因素
initial capacity
[初始容量]: 初始化时数组大小capacity
: 数组的大小
load factor
[负载因子]: 超过会扩容为原数组的 2 倍- 通常来说,default
load factor
=0.75
最合适,考虑时间 & 空间成本- 负载因子过高,可以降低空间成本,但是查找成本会提高
- 通常来说,default
非线程安全
遵循
fail-fast
机制
2. 类图
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
// ......
}
3. 属性
private static final long serialVersionUID = 362498820763181265L;
/*
* Implementation notes.
*
*
* 为避免每个 Node 下链表过长,造成 O(N) 复杂度,链表会转换成红黑树,复杂度降低为 O(log N)
*
* 由于树节点约为 2 倍常规链表结点的大小,因此只有当链表长度达到 TREEIFY_THRESHOLD = 8 时才会进行转换
* 当数量[移除节点 | resize 过程]减少到一定程度后,红黑树会退化成链表
* 理想状态下,在随机 hashCodes 作用下,若负载因子为 0.75,节点下的链表个数符合均值为 0.5 的泊松分布
* (http://en.wikipedia.org/wiki/Poisson_distribution)
* 以下为链表不同节点数量下的概率 -
* 可以看出,链表长度超过 8 的概率已经特别小了,基本不可能出现
*
* 0: 0.60653066
* 1: 0.30326533
* 2: 0.07581633
* 3: 0.01263606
* 4: 0.00157952
* 5: 0.00015795
* 6: 0.00001316
* 7: 0.00000094
* 8: 0.00000006
* more: less than 1 in ten million
*/
/**
* 初始 capacity 必须 为 2 的幂
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // == 16
/**
* The maximum capacity 必须 为 2 的幂 && <= 1<< 30
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* 若构造函数中未指定负载因子,则默认为 0.75f
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* 当链表节点个数至少为 TREEIFY_THRESHOLD 时,此时再添加节点,链表就会转换为树
* TREEIFY_THRESHOLD 必须 > 2 && 至少应该 == 8 才能满足上面的公式
*/
static final int TREEIFY_THRESHOLD = 8;
/**
* 链表节点个数 <= 6 时退化为链表,该值至少 == 6 才能满足上面的公式
*/
static final int UNTREEIFY_THRESHOLD = 6;
/**
* 链表转换为树时,数组长度至少为64,否则会先进行数组的扩容操作
* 为了避免在哈希表建立初期,多个键值对恰好被放入了同一个链表中而导致不必要的转化
* 该值至少为 4 * TREEIFY_THRESHOLD
*/
static final int MIN_TREEIFY_CAPACITY = 64;
/**
* 添加扰动,使其同时具备高位 & 低位特征
* 防止低几位出现想同的概率太大,尽可能的将数据实现均匀分布
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
/**
* 基于给定 capacity ,保证输出为 2 的幂
* 首先将 一个数字的二进制,从第一个不为0的位开始,把后面的所有位都设置成1
* 再 把Step 1得到的数值 +1,变成 2 的幂
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
/**
* 数组长度始终为 2 的幂
*/
transient Node<K,V>[] table;
/**
* 遍历时用到的 entrySet
*/
transient Set<Map.Entry<K,V>> entrySet;
/**
* KV 对的个数
*/
transient int size;
/**
* 保证 iterator 时的 fast-fail
*/
transient int modCount;
/**
* 阈值 = (capacity * load factor)
*/
int threshold;
/**
* 负载因子
*/
final float loadFactor;
4. 内部类
/**
* 单向链表类 (See below for
* TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
*/
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
5. 构造函数
/**
* 无参构造函数
* load factor = 0.75
* 该构造函数和其他的均不同,初始化时,未设置 初始容量,需要 resize() 扩容时再进行判断
* resize() 时,会设置 capacity = 16, threshod = 12
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
/**
* 指定 初始 capacity 构造函数,load factor = 0.75
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* 指定 初始 capacity & load factor 构造函数
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
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)) // loadFactor 判断
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity); // 根据 initialCapacity 确定数组大小,之后 resize() 时确定 扩容阈值
}
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
*/
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR; // 负载因子为 默认值
putMapEntries(m, false);
}
/**
* Implements Map.putAll and Map constructor
*
* @param m the map
* @param evict false when initially constructing this map, else
* true (relayed to method afterNodeInsertion).
*/
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // 原有数组为空,根据插入 map 元素个数设置 当前 map 的容量阈值
float ft = ((float)s / loadFactor) + 1.0F; // 根据原数组长度 & 负载因子 反推 容量阈值
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t); // 将 求得的 threshold 设置为 2 的幂
} else if (s > threshold) {
resize(); // 数组扩容
}
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
6. 常用方法
1. get(Object key)
@Override
public V getOrDefault(Object key, V defaultValue) {
Node<K,V> e = getNode(hash(key), key);
return e == null ? defaultValue : e.value;
}
/**
* 返回 期望值 | null
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* 根据给定 hash 判断是否存在 key 节点
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab = table;
Node<K,V> first;
Node<K,V> e;
int n;
K k;
if (tab != null
&& (n = tab.length) > 0
&& (first = tab[(n - 1) & hash]) != null) {
k = first.key;// 该 hash 对应数组下标的首节点的 key 值
if (first.hash == hash && // 要找的就是这个 首节点,返回
(k == key || (key != null && key.equals(k))))
return first;
e = first.next;
if (e != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key); // 红黑树的查找
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null); // 链表的查找
}
}
return null;
}
2. put(K key, V value)
/**
* 添加节点,若该节点已存在,则覆盖原值
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return old val | null
*/
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 == true 则不覆盖原值; onlyIfAbsent == false 则覆盖原值
* @param evict == false 则数组在创建模式中; evict == true 则数组不在创建中
* @return 旧val | null
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
Node<K,V>[] tab;
Node<K,V> p;
int n, i;
// 1) 数组为空,则首先进行初始化操作
tab = table;
n = tab.length;
if (tab == null || n == 0) {
tab = resize();
n = tab.length;
}
// 2) 当前 hash 所对应下的节点为空,则在此处创建新节点
i = (n - 1) & hash; // 获取该 hash 对应数组的具体下标,如果存在某个值,那么一定在这个下标处
p = tab[i]; // 定位到想要下标处的节点
if (p == null)
tab[i] = newNode(hash, key, value, null); // 为空,直接插入
else {
// 3) 当前 index 处已有节点
Node<K,V> e; // 要插入元素对应的 map 中已有的节点
K k;
k = p.key;
if (p.hash == hash &&
((k == 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) {
e = p.next;
if (e == null) {
p.next = newNode(hash, key, value, null); // 说明对应下标处的链表中也不存在,是个新节点
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
k = e.key;
if (e.hash == hash &&
(k == 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;
}
/**
* 数组的初始化 | 扩容
*
* @return the table
*/
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) { // oldTab != null 说明 该方法已经被执行过,例如 指定map的构造函数 | put() 等操作
if (oldCap >= MAXIMUM_CAPACITY) { // 原有容量已经达到峰值,无法扩容为原来的 2 倍,直接 return
threshold = Integer.MAX_VALUE;
return oldTab;
} else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY) // 可以扩容为原来的 2 倍 && 原来容量至少为 16
newThr = oldThr << 1; // newCap & newThr 扩容为原来的 2 倍
} else if (oldThr > 0) // oldTab == null && 在构造时指定了非零的 threshold
newCap = oldThr;
else { // oldTab == null && (无参构造函数 || 有参构造函数 且 threshod == 0)
newCap = DEFAULT_INITIAL_CAPACITY; // 数组大小 = 16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 扩容阈值 = 12
}
// 1) 可以扩容为原来的 2 倍 && 原来容量至少为 16 --> 不满足
// 2) oldTab == null && 在构造时指定了非零的 threshold
// 此时,新扩容阈值 == newCap * loadFactor
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 = oldTab[j];
if (e != null) {
// 原数组该 index 处有节点,需要进行迁移
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; // head 为头结点,tail 为当前节点,由于尾插,所以始终为尾节点
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
// 链表迁移中,没有重新计算每个节点下标 e.hash & (newCap - 1)
// 而是通过 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);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
/**
* 链表转换为红黑树
*/
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index;
Node<K,V> e;
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize(); // 数组长度小于 MIN_TREEIFY_CAPACITY [64] 时会先进行扩容而非转换为红黑树
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p; // 保存双向链表结构
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
// For treeifyBin
TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
return new TreeNode<>(p.hash, p.key, p.value, next);
}
void afterNodeAccess(Node<K,V> p) { } // 为 LinkedHashMap 重写该方法做准备,下同
void afterNodeInsertion(boolean evict) { }
3. remove(Object key)
/**
* Removes the mapping for the specified key from this map if present.
*
* @param key key whose mapping is to be removed from the map
* @return old val | null
*/
public V remove(Object key) {
Node<K,V> e = removeNode(hash(key), key, null, false, true));
return (e == null ? null : e.value;
}
/**
* Implements Map.remove and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to match if matchValue, else ignored
* @param matchValue if true only remove if value is equal
* @param movable if false do not move other nodes while removing
* @return the node, or null if none
*/
final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) {
Node<K,V>[] tab = table;
int n = tab.length;
int index = (n - 1) & hash;
Node<K,V> p = tab[index]; // 数组当前 index 下首节点
if (tab != null && n > 0 && p != null) { // 要删除的下标位置有元素,因此可能有要删除的节点
Node<K,V> node = null; // key 对应的 节点
Node<K,V> e; // 链表遍历时的当前节点
K k;
V v;
// 找到要删除的节点 node
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
k = e.key;
if (e.hash == hash &&
(k == key || (key != null && key.equals(k)))) {
node = e;
break;
}
p = e; // 暂存上一个节点
} while ((e = e.next) != null);
}
}
// 移除 node 节点
v = node.value;
if (node != null && (!matchValue || v == value || (value != null && value.equals(v)))) {
if (node instanceof TreeNode) // 删除树节点
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next; // 删除的是链表首节点
else
p.next = node.next; // 删除链表非首节点
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
void afterNodeRemoval(Node<K,V> p) { }
4. 遍历用
1) entrySet()
/**
* 返回 k-v 对的集合
* 对该集合进行修改会影响对 map 自身的修改,反之亦然
* 提供 通过 set.remove() iterator.remove() 方法对元素进行删除
* Iterator 不提供 add() 方法,HashMap 也没有对其进行扩展,所以无法在遍历时进行增加操作
* 不支持 通过 add() 对元素进行添加
*
* @return a set view of the mappings contained in this map
*/
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es = entrySet;
return es == null ? (entrySet = new EntrySet()) : es;
}
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
public final Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator();
}
public final boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(hash(key), key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Object value = e.getValue();
return removeNode(hash(key), key, value, true, true) != null;
}
return false;
}
public final Spliterator<Map.Entry<K,V>> spliterator() {
return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
tab = table;
if (size > 0 && tab != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
interface Entry<K,V> {
K getKey();
V getValue();
boolean equals(Object o);
int hashCode();
}
2) keySet()
/**
* 返回 key 集合
* 这个集合基于 map,因此对 map 的修改会影响到 set,反之亦然
* set 集合提供对 map 的 删除、清空等操作,但没有 add() 操作
*
* @return a set view of the keys contained in this map
*/
public Set<K> keySet() {
Set<K> ks = keySet;
if (ks == null) {
ks = new KeySet(); // 第一次获取时,若该类还没有进行初始化,先进行初始化操作
keySet = ks;
}
return ks;
}
final class KeySet extends AbstractSet<K> {
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
public final Iterator<K> iterator() { return new KeyIterator(); }
public final boolean contains(Object o) { return containsKey(o); }
public final boolean remove(Object key) {
return removeNode(hash(key), key, null, false, true) != null;
}
public final Spliterator<K> spliterator() {
return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super K> action) {
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
tab = table;
if (size > 0 && tab != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e.key);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
3) values()
/**
* 返回 value 集合
* 对 values 类所做的操作会反馈到 map 上,反之亦然
* 提供 通过 iterator() 进行元素删除操作
* 不支持 add() 操作
*
* @return a view of the values contained in this map
*/
public Collection<V> values() {
Collection<V> vs = values;
if (vs == null) {
vs = new Values();
values = vs;
}
return vs;
}
final class Values extends AbstractCollection<V> {
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
public final Iterator<V> iterator() { return new ValueIterator(); }
public final boolean contains(Object o) { return containsValue(o); }
public final Spliterator<V> spliterator() {
return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super V> action) {
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
tab = table;
if (size > 0 && tab != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e.value);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
7. HashMap VS HashTable
HashMap | HashTable | |
---|---|---|
线程安全 | 🥶 | 😋 |
初始容量 | 16 | 11 |
扩容 | 2n | 2n + 1 |
key == null | 😋 | 🥶 |
底层数据结构 | 数组 + 链表 + 红黑树(JDK 1.8) | 数组 + 链表 |