HashMap
只实现了Map接口,没有实现Comparable接口。
hashmap采用数组+链表(可转换成红黑树)的存储结构。
capacity是指数组的容量,threshold是指map中元素个数size的临界值,树化条件:1.数组容量 达到 min_treeify_capcaity2.链表长度达到 treeify_threshold时候会将链表转换成红黑树。
构造方法
无参构造方法采用默认值,而指定了初始容量的构造方法会将初始容量存储在阈值threshold中。(这样做与扩容方法有关)
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))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
链表容量保证为2^n
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;
}
计算元素所在槽
tab[(n - 1) & hash]
计算哈希
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
-
为什么不直接用hashcode?而要在hashcode的基础上再一次hash?
因为hashcode是32位int,而n = tablesize往往很小,如果直接用hashcode&(n-1)则会浪费hashcode的高位部分,导致分布不均匀。所以将高位与低位部分异或。来使得高位与低位都参与计算,降低碰撞
-
hash & (n-1) 等于hash%n, n 为二进制幂
假设n等于2^t.则1在第t+1位。因此n-1就是相当于从第一位到t位都是1。即与上n-1等于取hash的1到t位。
增
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;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// p指代当前节点
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//e 指代应该插入节点的位置。
//检查头结点 e != null
if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//红黑树的情况 e根据是否存在key对应的节点返回NULL或者原节点
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
//遍历到末尾了,直接在链表末尾插入当前节点。这里e == null。先插入再树化/扩容
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果发现链表中已经存在等待插入的节点, e != null
if (e.hash == hash &&
((k = e.key) == key |(key!=null&&key.equals(k))))
break;
p = e;
}
}
//存在节点,直接更新val值即可。
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;
}
红黑树中put。key需要实现Comparable接口
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;
//dir表示插入左子树还是右子树
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;
//hash相等,key不等。用KC比
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;
}
//子树里也没有。而且当前节点用kc也比不了。就比较类的名字。
dir = tieBreakOrder(k, pk);
}
//找到了插入的方向dir,开始插入。xp指代x的父母,X 指代新节点
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;//表示插入之前红黑树没有该节点
}
}
}
//找出红黑树中对应key的节点,如果不存在返回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;
//如果kc为空。则瞎几把搜索。先搜右子树
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
扩容
扩容在put 时候进行,1种是数组为空时候,另一种是size达到了threshold时候的扩容。
在构造方法执行时候,并没有初始化数组,当插入第一个元素时候才会开始初始化(扩容)数组。
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
}
//数组不存在,即尚未初始化,对应2种情况,一种是构造方法指定了初始容量
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
//另一种是没有指定初始容量,初始阈值,则采用默认值。
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) {
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)
((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;
//搬家的新位置分2种,因为容量是2倍增长,所以链表中的元素的新位置要么在原位置,要么在原位置+oldcap的新位置。分别用lo与hi指代这2个链表。
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;
}
可以看到多线程扩容情况下不会出现1.7版本的死循环问题。但是依然不安全。搞不懂揪着死循环不放干啥,hashmap本来就不是给并发用的,没有死循环也有其他安全问题。
删除
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);
}
}
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;
}
链表与红黑树的转换
红黑树节点类,继承了LinkedHashMap.Entry类,which继承了HashMap.Node
链表-> 红黑树:put时候,如果发现放置完当前元素后,链表长度(bincount) == threshold。则调用treefiBin转换成红黑树。而该方法会检查数组容量是否达到MIN_TREEIFY_CAPACITY。如果没达到,则直接扩容返回。put时候,如果发现
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 void treeify(Node<K,V>[] tab) {
TreeNode<K,V> root = null;
for (TreeNode<K,V> x = this, next; x != null; x = next) {
next = (TreeNode<K,V>)x.next;
x.left = x.right = null;
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
else {
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null)||
(dir = compareComparables(kc, k, pk)) ==0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);//平衡红黑树
break;
}
}
}
}
moveRootToFront(tab, root);
}
/**
* Ensures that the given root is the first node of its bin.
*/
static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
int n;
if (root != null && tab != null && (n = tab.length) > 0) {
int index = (n - 1) & root.hash;
TreeNode<K,V> first = (TreeNode<K,V>)tab[index];
if (root != first) {
Node<K,V> rn;
tab[index] = root;
TreeNode<K,V> rp = root.prev;
if ((rn = root.next) != null)
((TreeNode<K,V>)rn).prev = rp;
if (rp != null)
rp.next = rn;
if (first != null)
first.prev = root;
root.next = first;
root.prev = null;
}
assert checkInvariants(root);
}
}
红黑树->链表
红黑树中的节点因为有prev与next 指针,所以拆分还是比较简单的。
在resize的搬家时候如果检测到是红黑树节点则会先拆分成链表,再搬家。理想情况下链表长度会缩短一半。然后等到以后插入发现链表长度达到新threshold才会进行转换成红黑树的操作。
可以看到拆分的时候链表内节点的相对顺序并没有改变。
拆分成链表并没有损坏红黑树结构,因此拆分完就需要调用untreeify来将红黑树节点转换成链表节点。有个优化如果发现hitail == null则说明没有拆成2个链表,树结构不改变,无需untreeify。
final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
TreeNode<K,V> b = this;
// Relink into lo and hi lists, preserving order
TreeNode<K,V> loHead = null, loTail = null;
TreeNode<K,V> hiHead = null, hiTail = null;
int lc = 0, hc = 0;
for (TreeNode<K,V> e = b, next; e != null; e = next) {
next = (TreeNode<K,V>)e.next;
e.next = null;
if ((e.hash & bit) == 0) {
if ((e.prev = loTail) == null)
loHead = e;
else
loTail.next = e;
loTail = e;
++lc;
}
else {
if ((e.prev = hiTail) == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
++hc;
}
}
if (loHead != null) {
//
if (lc <= UNTREEIFY_THRESHOLD)
tab[index] = loHead.untreeify(map);
else {
tab[index] = loHead;
if (hiHead != null) // (else is already treeified)
loHead.treeify(tab);
}
}
if (hiHead != null) {
if (hc <= UNTREEIFY_THRESHOLD)
tab[index + bit] = hiHead.untreeify(map);
else {
tab[index + bit] = hiHead;
if (loHead != null)
hiHead.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;
}
红黑树节点与链表节点的转换
TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
return new TreeNode<>(p.hash, p.key, p.value, next);
}
查找
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) {
//会调用TreeNode类的find方法
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;
}
总结
- 对hash的优化方法,使得节点尽可能均匀分布
- 数组容量是2的幂次方,简化取模运算(相当于与上capacity-1)
- 扩容时候链表选择2倍长度,使得新位置计算更迅速(要么原地,要么偏移old capacity)
- 引入红黑树,来缓解当链表长度变长带来的性能下降
- 红黑树带来的问题:
- 一旦扩容需要拆分红黑树以及untreeify带来的性能消耗
- 当不断删除导致map中节点很少时,红黑树查找节点的性能提升抵不上reblance带来的性能下降
- 增加MIN_TREEIFY_CAPACITY,当数组较小时优先选择扩容数组而不是树化
- 在hashmap的节点有
- 实现了Map.Entry接口的HashMapNode节点,含有next链接
- 扩展了LinkedHashMap.Entry节点的TreeNode节点,含有parent,left,right,prev链接。ps:LinkedhashMap.Entry节点含有before,after链接,而LinkedHashMap.Entry继承了HashMap.Node。
- 下列更改存储数据结构的操作,保留了节点相对顺序(不改变next链接)。事实上,只有TreeNode类中的moveRootToFront操作会改变root节点的相对顺序。包括:
- 扩容操作
- 树化操作(除了红黑树的根节点,为了保证红黑树根处于桶的头部)
- 链表节点删除操作(红黑树节点的删除如果涉及到根节点的变化,则会改变节点相对顺序)
- 拆分树操作
- 逆树化操作