前言
HashMap在面试中经常会问到,包括它的底层实现,与其他容器的比较以及1.8前后实现的比较。
HashMap的简要介绍
在JDK1.8中,HashMap的实现方式是数组+链表/红黑树,存储的是无序键值对映射,非线程安全。
HashMap采用数组来存储key、value构成的Entry对象,无容量限制,其默认初始数组大小为16,负载因子为0.75,hash冲突时采用链表解决冲突,若链表长度超过8则将其转为红黑树,红黑树中节点少于6时转为链表。(不过源码中也提到了在负载因子等于0.75的情况下,链表长度超过8的可能性极低),如果存放的key是自定义类,则需要重写hashcode和equal方法。

由图可得出其继承关系分别是继承自AbstractMap,实现了Cloneable、Map、Serializable接口。
从源码分析HashMap
成员变量
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;//初始默认底层数组大小为16
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认装载因子为0.75
static final int TREEIFY_THRESHOLD = 8;//树化阈值
static final int UNTREEIFY_THRESHOLD = 6;//链表化阈值
static final int MIN_TREEIFY_CAPACITY = 64;//数组大于这个值才会发生链表转红黑树
transient Node<K,V>[] table;//HashMap的底层实际就是这个数组,大小要求是2的幂次方
transient int size;//容器内键值对映射的数量
//以下是实际存储节点的类
//链表节点
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;
}
}
//红黑树节点
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
TreeNode(int hash, K key, V val, Node<K,V> next) {
super(hash, key, val, next);
}
/**
* Returns root of tree containing this node.
*/
final TreeNode<K,V> root() {
for (TreeNode<K,V> r = this, p;;) {
if ((p = r.parent) == null)
return r;
r = p;
}
}
构造方法
//HashMap的构造方法有4种,着重看这个
public HashMap(int initialCapacity, float loadFactor) {//传入初始大小和装载因子
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)//超过最大值就用2的30次方
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);//将初始容量设置为满足传入的容量的2的幂次方
}
构造方法一共有4种,分别是:无参、传入初始容量、传入初始容量和装载因子、传入一个Map容器,然后还有以下的操作来保证HashMap的一些特性。
//前面构造函数有这个,通过一系列右移再 或 实现容量为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;
}
以上一系列的0填充右移再或的操作能使容量为2的幂次方。
//HashMap的hash方法也是它的特性点之一
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
index = (n - 1) & hash;//下标计算的方法
一般我们提到hash方法,最先想到的应该是求模运算,均匀地分布到桶里,但是求模运算效率太低,HashMap进行了优化,HashMap 中则通过 h&(length-1)的方法来代替取模,同样实现了均匀的散列,但效率要高很多。
回想前面为什么对桶的要求一定要是2的整数次幂呢?原因就在这,首先,length为2的整数次幂的话, h&(length-1)就相当于对 length 取模, 这样便保证了散列的均匀, 同时也提升了效率;其次, length 为2的整数次幂的话,为偶数,这样 length-1 为奇数,奇数的最后一位是 1,这样便保证了 h&(length-1)的最后一位可能为0,也可能为 1 (这取决于 h 的值),即与后的结果可能为 偶数, 也可能为奇数, 这样便可以保证散列的均匀性, 而如果 length 为奇数的话, 很明显 length-1 为偶数, 它的最后一位是0,这样 h&(length-1)的最后一位肯定为0,即只能为偶 数, 这样任何 hash 值都只会被散列到数组的偶数下标位置上,这便浪费了近一半的空间, 因此,length 取2的整数次幂,是为了使不同 hash 值发生碰撞的概率较小,这样就能使元 素在哈希表中均匀地散列。
添加
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;//table,node指针,length,node index这四个
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
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))))//key相同的话
e = p;//直接替换value
else if (p instanceof TreeNode)//key不同,但是是红黑树节点
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);//没找到所以加一个新链表节点
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);//加入后要检查一下是否要树化
break;
}
if (e.hash == hash &&//这里说明找到了key,直接替换value
((k = e.key) == 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;
删除
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;
//如果hash对应的index就是要找的key,然后就找到了这个元素
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 {//用do while遍历链表找到链表节点
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
//找到后,根据找到的位置进行红黑树删除节点或者链表删除或者桶里移除
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;
}
扩容
扩容函数,如果hash桶为空,初始化默认大小,否则双倍扩容。扩容是2的倍数,根据hash桶的计算方法,元素hashcode值不变,所以元素在新hash桶的下标,要么跟旧的一样,要么乘2。
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) {
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;//新容量实际是大于阈值的2的整数次幂(通过后面的操作实现的)
else { // zero initial threshold signifies using defaults
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) {//这个桶里有元素
//如果该位置没有形成链表, 则再次计算 index, 放入新 table
//假设扩容前的 table 大小为 2 的 N 次方, 有上述 put 方法解析可知,
//元素的 table 索引为其 hash 值的后 N 位确定
//那么扩容后的 table 大小即为 2 的 N+1 次方, 则其中元素的 table 索引
//为其 hash 值的后 N+1 位确定, 比原来多了一位
//因此, table 中的元素只有两种情况:
//元素 hash 值第 N+1 位为 0: 不需要进行位置调整
//元素 hash 值第 N+1 位为 1: 调整至原索引的两倍位置
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;
else if (e instanceof TreeNode)// 如果该位置形成了红黑树,则split
((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;
// 用于确定元素 hash 值第 N+1 位是否为 0:若为 0, 则使用 loHead 与
//loTail, 将元素移至新 table 的原索引处;若不为 0, 则使用 hiHead 与
//hiHead, 将元素移至新 table 的两倍索引处
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;
}
更新和查询
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
public boolean containsValue(Object value) {
Node<K,V>[] tab; V v;
if ((tab = table) != null && size > 0) {
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next) {
if ((v = e.value) == value ||
(value != null && value.equals(v)))
return true;
}
}
}
return false;
}
//实际上查询都是通过这个getNode方法
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 &&//table不为空
(first = tab[(n - 1) & hash]) != null) {//且table对应的index不为空
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)//检测到如果是一个红黑树节点,就从用getTreeNode找
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;
}
遍历
遍历是先遍历table找到不为空的桶,如果桶内元素的next为空就继续遍历桶,不为空就遍历next(链表或红黑树)
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();
if (size > 0 && (tab = table) != 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();
}
}
}
abstract class HashIterator {
Node<K,V> next; // next entry to return
Node<K,V> current; // current entry
int expectedModCount; // for fast-fail
int index; // current slot
HashIterator() {
expectedModCount = modCount;
Node<K,V>[] t = table;
current = next = null;
index = 0;
if (t != null && size > 0) { // advance to first entry
do {} while (index < t.length && (next = t[index++]) == null);
}
}
public final boolean hasNext() {
return next != null;
}
final Node<K,V> nextNode() {
Node<K,V>[] t;
Node<K,V> e = next;
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (e == null)
throw new NoSuchElementException();
if ((next = (current = e).next) == null && (t = table) != null) {
do {} while (index < t.length && (next = t[index++]) == null);
}
return e;
}
public final void remove() {
Node<K,V> p = current;
if (p == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
current = null;
K key = p.key;
removeNode(hash(key), key, null, false, false);
expectedModCount = modCount;
}
}
final class KeyIterator extends HashIterator
implements Iterator<K> {
public final K next() { return nextNode().key; }
}
红黑树相关操作(可以跳过直接看总结)
链表转红黑树
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();
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);
}
}
红黑树转链表
final Node<K,V> untreeify(HashMap<K,V> map) {
Node<K,V> hd = null, tl = null;
for (Node<K,V> q = this; q != null; q = q.next) {
Node<K,V> p = map.replacementNode(q, null);
if (tl == null)
hd = p;
else
tl.next = p;
tl = p;
}
return hd;
}
红黑树添加
final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
int h, K k, V v) {
Class<?> kc = null;
boolean searched = false;
TreeNode<K,V> root = (parent != null) ? root() : this;
for (TreeNode<K,V> p = root;;) {
int dir, ph; K pk;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
Node<K,V> xpn = xp.next;
TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
if (dir <= 0)
xp.left = x;
else
xp.right = x;
xp.next = x;
x.parent = x.prev = xp;
if (xpn != null)
((TreeNode<K,V>)xpn).prev = x;
moveRootToFront(tab, balanceInsertion(root, x));
return null;
}
}
}
红黑树删除
final void removeTreeNode(HashMap<K,V> map, Node<K,V>[] tab,
boolean movable) {
int n;
if (tab == null || (n = tab.length) == 0)
return;
int index = (n - 1) & hash;
TreeNode<K,V> first = (TreeNode<K,V>)tab[index], root = first, rl;
TreeNode<K,V> succ = (TreeNode<K,V>)next, pred = prev;
if (pred == null)
tab[index] = first = succ;
else
pred.next = succ;
if (succ != null)
succ.prev = pred;
if (first == null)
return;
if (root.parent != null)
root = root.root();
if (root == null
|| (movable
&& (root.right == null
|| (rl = root.left) == null
|| rl.left == null))) {
tab[index] = first.untreeify(map); // too small
return;
}
TreeNode<K,V> p = this, pl = left, pr = right, replacement;
if (pl != null && pr != null) {
TreeNode<K,V> s = pr, sl;
while ((sl = s.left) != null) // find successor
s = sl;
boolean c = s.red; s.red = p.red; p.red = c; // swap colors
TreeNode<K,V> sr = s.right;
TreeNode<K,V> pp = p.parent;
if (s == pr) { // p was s's direct parent
p.parent = s;
s.right = p;
}
else {
TreeNode<K,V> sp = s.parent;
if ((p.parent = sp) != null) {
if (s == sp.left)
sp.left = p;
else
sp.right = p;
}
if ((s.right = pr) != null)
pr.parent = s;
}
p.left = null;
if ((p.right = sr) != null)
sr.parent = p;
if ((s.left = pl) != null)
pl.parent = s;
if ((s.parent = pp) == null)
root = s;
else if (p == pp.left)
pp.left = s;
else
pp.right = s;
if (sr != null)
replacement = sr;
else
replacement = p;
}
else if (pl != null)
replacement = pl;
else if (pr != null)
replacement = pr;
else
replacement = p;
if (replacement != p) {
TreeNode<K,V> pp = replacement.parent = p.parent;
if (pp == null)
root = replacement;
else if (p == pp.left)
pp.left = replacement;
else
pp.right = replacement;
p.left = p.right = p.parent = null;
}
TreeNode<K,V> r = p.red ? root : balanceDeletion(root, replacement);
if (replacement == p) { // detach
TreeNode<K,V> pp = p.parent;
p.parent = null;
if (pp != null) {
if (p == pp.left)
pp.left = null;
else if (p == pp.right)
pp.right = null;
}
}
if (movable)
moveRootToFront(tab, r);
}
红黑树查询
final TreeNode<K,V> getTreeNode(int h, Object k) {
return ((parent != null) ? root() : this).find(h, k, null);
}
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
TreeNode<K,V> p = this;
do {
int ph, dir; K pk;
TreeNode<K,V> pl = p.left, pr = p.right, q;
if ((ph = p.hash) > h)
p = pl;
else if (ph < h)
p = pr;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if (pl == null)
p = pr;
else if (pr == null)
p = pl;
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
总结
HashMap这一部分啰嗦了很多,但因为面试基本就问这玩意,所以多放点源码还是很有必要的。面试可能会问HashMap跟HashTable的区别,跟HashSet的区别,底层实现(JDL1.7和JDK1.8中的区别)容量为什么要是2的整数次幂,hash计算的方法,还有多线程下的问题(其解决方法是用HashTable或者ConCurrentHashMap)。
以上是HashMap在面试中可能会被问到的角度,经过源码的学习想必已经能轻松回答出来了,最后再来总结一下HashMap,HashMap底层是数组+链表/红黑树实现,hash的计算方法是hashcode ^(异或)hashcode右移16位{高位异或低位},桶的下标是hash&(与)容量减一(n-1),{实际就是hash%n},但&比%更高效,然后就是HashMap的一系列操作,从构造,到添加删除更新查询,这些操作对红黑树节点和链表节点稍有差别,遍历的话,可通过keySet(),entrySet()等来遍历。