说明
JDK版本jdk1.8.0_191
1. 核心数据结构
首先了解一下继承体系:
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
public abstract class AbstractMap<K,V> implements Map<K,V>
再看看内部数据结构表示;
HashMap内部维护了一个table字段,我们简单称之为表,这个表是一个节点数组
。
table字段的说明:
table首次使用的时候才初始化,并且在必要的时候会改变大小(resize)。当被初始化分配内存时,它的长度应该总是2的幂(在某些操作下可以容许长度为0)
/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;
再看Node数据结构:
/**
* Basic hash bin node, used for most entries. (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;
}
}
Node数据结构很简单,主要点就在那4个字段
上,函数很简单。
Node是一个hash节点。通过字段Node<K,V> next;
,可以知道,该数据结构是一个链表
。
也就是说HashMap的数据结构
可以用如下的图形来表示,首先是一个table,表示Node数组,而数组的每个元素都是一个Node链表。也就是说
table数组中每个位置的元素都被当成一个桶,每个桶存放一个链表,而同一个链表存放hash值与散列桶取模运算结果相同的节点
很显然,查找操作为:
计算键值对所在的桶
在链表上进行顺序查找
2. HashMap#put
现在分析HashMap#put
。
放入key-value键值对到map中,如果map中已经存在了此key,旧的value将会被替换成新的。返回值为被替换的value值;或者当此key之前并不存在,返回null;亦或者之前存在着键值对key->null
/**
* 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);
}
先计算hashcode并将高位哈希码传播到低位。由于table使用的是2的幂掩码,只变化在当前掩码之上的位的哈希集总是会引起冲突。(例如小表中的连续浮点数key)。所以我们做一个改变:将高位的影响传播到低位。这样能做到速度,功用,位传播质量的权衡。
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* quality of bit-spreading. Because many common sets of hashes
* are already reasonably distributed (so don't benefit from
* spreading), and because we use trees to handle large sets of
* collisions in bins, we just XOR some shifted bits in the
* cheapest possible way to reduce systematic lossage, as well as
* to incorporate impact of the highest bits that would otherwise
* never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
这里的hash函数,我们可以称之为 “扰动函数"
,由于hashcode是一个int类型的值,它的大小为[-2^31, 2^31-1]
,如果我们直接拿他作为hash key的话,显然很难发生冲突,但是计算机的容量显然没有这么大。
现在使用的方法就是将其右移16位,再与自身做异或运算
,显然,这将高16位,低16位都参与了运算,并且尽量使低16位混淆。由于table的长度总是2的整数次幂,2的整数次幂有个性质,就是hash % n就相当于hash & (n-1),其中n表示table长度
,这样,通过与运算,得到了数组的下标。
再看put方法调用的子方法:HashMap#putVal
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
// tab就是table字段的引用
// n为table的长度
// i 为 (n - 1) & hash
// 也就是由hash所确定的桶在table中的位置
// p节点就是上面i所确定的桶的节点,也可以认为是桶确定的链表的头节点
Node<K,V>[] tab; Node<K,V> p; int n, i;
// 之前在分析table字段时说过,table是延迟初始化
// 也就是在首次使用的时候才初始化
// 这里resize后面再讨论,我们现在知道,这个是一个初始化操作
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 如果hash所确定的桶在table中的位置还没有被占过位置
// 那么我们需要给它分配一个Node空间,并且占位
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else { // hash所确定的桶已经存在位置了
// 那么根据我们之前对数据结构的分析,此时应该将节点插入到此桶确定的链表中
// e就是插入后的返回的节点(或者将要替换的节点)
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)
// 如果是红黑树表示的节点的话
// TODO 这里我们先不讨论,后面专门进行讨论
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
// 如果是链表形式的节点
// 开始遍历链表
for (int binCount = 0; ; ++binCount) {
// 如果已经遍历到链表尾部了
if ((e = p.next) == null) {
// 将节点插入到尾部
p.next = newNode(hash, key, value, null);
// 如果达到threshold,那么就将hash所确定的桶链表树形化
// TODO 这里也根上面一样,先不讨论
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
// 一个个节点查找,如果找到了,那么直接退出循环
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
// 没找到,继续接着迭代着找
p = e;
}
}
// 此时e如果找到了的话,应该是待替换值的节点引用
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value; // 替换
afterNodeAccess(e);
return oldValue;
}
}
// modCount++表示对HashMap进行了修改,fail-fast需要用到
++modCount;
// 如果大小已经超过允许的阈值,则resize
if (++size > threshold)
resize();
// 节点插入之后的操作
afterNodeInsertion(evict);
return null;
}
上面有两个细节我们暂时没有讨论到,就是resize函数
,还有跟Tree相关的代码段:p instanceof TreeNode
和treeifyBin(tab, hash)
总的来讲put方法的逻辑如下:
而对于resize
,下面讨论,也就是我们常说的HashMap的扩容
3. HashMap#resize
说到HashMap的扩容,我们就需要了解HashMap中的几个参数字段:
参数 | 含义 |
---|---|
capacity | table 的容量大小,默认为 16。需要注意的是 capacity 必须保证为 2 的 n 次方 |
size | 键值对数量 |
threshold | size 的临界值,当 size 大于等于 threshold 就必须进行扩容操作 |
loadFactor | 装载因子,table 能够使用的比例,threshold = capacity * loadFactor |
/**
* The default initial capacity - MUST be a power of two.
*/
// 默认初始容量(capacity),必须是2的幂,这里是16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
// 最大的容量,必须是2的幂,这里是2^31
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* The load factor used when none specified in constructor.
*/
// 默认负载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
// 当链表长度超过TREEIFY_THRESHOLD,桶的节点开始使用树来表示,而非链表。
// 至少添加节点到TREEIFY_THRESHOLD这么多数量,节点就被转化为树。
// 此值必须比2大,设置为8是为了契合节点数足够小的的时候重新缩水成链表(表示可能再移除元素就真的要缩成链表了)
static final int TREEIFY_THRESHOLD = 8;
/**
* The bin count threshold for untreeifying a (split) bin during a
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
*/
// 在resize操作时,由树转化为链表的界限值
// 此值应该比TREEIFY_THRESHOLD小,最大是6,用来契合移除元素时的缩水检测
static final int UNTREEIFY_THRESHOLD = 6;
/**
* The smallest table capacity for which bins may be treeified.
* (Otherwise the table is resized if too many nodes in a bin.)
* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
* between resizing and treeification thresholds.
*/
// 节点被树形化的最小表容量
// 至少时4倍TREEIFY_THRESHOLD,用来避免resize和树形化阈值冲突
static final int MIN_TREEIFY_CAPACITY = 64;
HashMap#resize
初始化或者双倍扩容表的大小。如果表还为null,则分配初始容量大小为threshold。否则,由于我们使用的是2的幂扩容,所以每个节点元素必须要么保持原索引,要么放在新表中的2的幂偏移位置。
/**
* Initializes or doubles table size. If null, allocates in
* accord with initial capacity target held in field threshold.
* Otherwise, because we are using power-of-two expansion, the
* elements from each bin must either stay at same index, or move
* with a power of two offset in the new table.
*
* @return the table
*/
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
// 旧容量
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 旧threshold阈值
int oldThr = threshold;
// 新阈值,新容量
int newCap, newThr = 0;
// oldCap > 0 说明是已经初始化过的
if (oldCap > 0) {
// 如果旧容量大于最大容量
if (oldCap >= MAXIMUM_CAPACITY) {
// 把阈值开到最大Integer.MAX_VALUE
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
// 新阈值 = 旧阈值 * 2
newThr = oldThr << 1; // double threshold
}
// oldCap <= 0,我们认为仍然没有初始化过
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
// 注意threshold为 load * cap
newCap = DEFAULT_INITIAL_CAPACITY;
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) {
// 遍历,把oldTable全部重新插入新的table中
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
// 释放旧的table值,旧的table值交由e引用
oldTab[j] = null;
// 将旧键值对”插入“到新的table中
// 如果链表为单节点
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 { // 原索引 + oldCap
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;
}
然而,扩容会引发如下问题,下面的rehash
也就是上面的重新插入到新的table中。
4. HashMap#get
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
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;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
// 如果桶根节点刚好命中
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
// 否则到链表中/红黑树中找
if ((e = first.next) != 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;
}
5. HashMap#remove
/**
* 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 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 remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
// 如果恰好匹配的就是桶节点
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 {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
// 找到了需要删除的节点node
if (node != null && (!matchValue || (v = node.value) == 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;
}