HashMap
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
成员变量
//默认初始容量为16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
//最大的容量为2^30
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认的加载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//若链表长度大于等于8就将链表转化为红黑树
static final int TREEIFY_THRESHOLD = 8;
//若红黑树长度小于等于6就转化为链表
static final int UNTREEIFY_THRESHOLD = 6;
//转化为树的最小的容量
static final int MIN_TREEIFY_CAPACITY = 64;
//HashMap使用table维护数据结构,长度由容量决定
transient Node<K,V>[] table;
//
transient Set<Map.Entry<K,V>> entrySet;
//元素的个数
transient int size;
//结构修改次数
transient int modCount;
//阀值 当元素个数大于threshold时,需要使用resize()扩容
int threshold;
//加载因子
final float loadFactor;
构造函数
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;
//返回一个比给定整数大且最接近的2的幂次方整数,如给定10,返回2的4次方16
this.threshold = tableSizeFor(initialCapacity);
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
}
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
数据的存取
put方法:
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;
if ((p = tab[i = (n - 1) & hash]) == null)
//如果hash所在位置为null,直接put
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))))//如果hash值和key值相等,将当前元素指向e(在下方修改value值)
e = p;
//如果为红黑树,则使用一下方法添加元素
else if (p instanceof TreeNode)
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)
// 链表节点数大于等于阈值8,调用treeifyBin方法,当
//tab.length大于64将链表改为红黑树
// 如果tab.length < 64或tab为null,则调用resize
//方法重构链表.
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null &&
key.equals(k))))
break;
p = e;
}
//链表中原本存在相同的key,则返回oldValue,重新赋值value
if (e != null) {
V oldValue = e.value;
//onlyIfAbsent为false,存在相同key时执行替换,true则不替
//换
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
//超过阀值则重新散列
resize();
afterNodeInsertion(evict);
return null;//原HashMap中不存在相同的key,插入键值对后返回null
}
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // pre-size
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t);
}
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);
}
}
}
//红黑树put节点
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;
//计算dir值判断放在树的左还是右
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
//key相等返回当前节点
else if ((pk = p.key) == k || (k != null &&
k.equals(pk)))
return p;
//hash值相等,但无法进行比较
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;
//如果当前节点还没有左孩子或者右孩子时才能插入
Node<K,V> xpn = xp.next;
//当前节点的后继给x做后继
TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
//判断新节点x放在当前节点的左还是右
if (dir <= 0)
xp.left = x;
else
xp.right = x;
//当前节点的后继为x
xp.next = x;
//x的父亲和前驱都是当前节点
x.parent = x.prev = xp;
if (xpn != null)
//如果当前节点的后继不为空,则后继节点的前驱是x
((TreeNode<K,V>)xpn).prev = x;
//红黑树插入元素后的平衡操作
moveRootToFront(tab, balanceInsertion(root, x));
return null;
}
}
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
public void putAll(Map<? extends K, ? extends V> m) {
putMapEntries(m, true);
}
get方法:
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 (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;
//红黑树查找节点
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)))
//若hash相等但是元素不等,左子树为空,查找右子树
else if (pl == null)
p = pr;
//若hash相等但是元素不等,左子树为空,查找左子树
else if (pr == null)
p = pl;
//若hash相等但是元素不等,左右子树都不为空,计算dir值判断查找
//左或者右子树
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
//若hash相等但是元素不等,左右子树都不为空,无法比较key的大小
//遍历右子树
else if ((q = pr.find(h, k, kc)) != null)
return q;
//若hash相等但是元素不等,左右子树都不为空,无法比较key的大小
//遍历右子树为空,遍历左子树
else
p = pl;
} while (p != null);
return null;
}
removed()方法:
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) {
//索引下第一个元素
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//链表首节点命中
node = p;
else if ((e = p.next) != null) {
//红黑树节点查找
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;
}
//红黑树节点删除
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 || 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 void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
//如果当前哈希表为空,或者哈希表中元素的个数小于 进行树形化的阈值(默认
//为 64),就去新建/扩容
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
//如果哈希表中的元素个数超过了 树形化阈值,进行树形化
// e 是哈希表中指定位置桶里的链表节点,从第一个开始
TreeNode<K,V> hd = null, tl = null;//头结点和尾节点
do {
//新建一个树形节点,内容和当前链表节点 e 一致
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);
}
}
TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
return new TreeNode<>(p.hash, p.key, p.value, next);
}
接下来是树型化:
//将TreeNode节点转化为红黑树
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;
//确定dir的值
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);
//把 当前节点变成 x 的父亲
//如果当前比较节点的哈希值比 x 大,x 就是左孩子,否
//则 x 是右孩子
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);
}
红黑树还原为链表就比较简单了:
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;
}
resize()方法
// 扩容兼初始化
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) {
// 原数组长度大于最大容量则将threshold设为Integer.MAX_VALUE
// 接近MAXIMUM_CAPACITY的两倍
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
// 新数组长度 是原来的2倍,
// 临界值也扩大为原来2倍
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in
//threshold
// 如果原来的thredshold大于0则将容量设为原来的thredshold
// 在第一次带参数初始化时候会有这种情况
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;
// 如果原来的table有数据,则将数据复制到新的table中
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;
//红黑树的树形结构修剪
((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;
// 注意:不是(e.hash & (oldCap-1));而是
//(e.hash & oldCap)
// (e.hash & oldCap) 得到的是 元素的在数组中
//的位置是否需要移动,示例如下
// 示例1:
// e.hash=10 0000 1010
// oldCap=16 0001 0000
// & =0 0000 0000 比较高位的第
//一位 0
//结论:元素位置在扩容后数组中的位置没有发生改变
// 示例2:
// e.hash=17 0001 0001
// oldCap=16 0001 0000
// & =1 0001 0000 比较高位的第一
//位 1
//结论:元素位置在扩容后数组中的位置发生了改变,
//新的下标位置是原下标位置+原数组长度
// (e.hash & (oldCap-1)) 得到的是下标位置,
//示例如下
// e.hash=10 0000 1010
// oldCap-1=15 0000 1111
// & =10 0000 1010
// e.hash=17 0001 0001
// oldCap-1=15 0000 1111
// & =1 0000 0001
//新下标位置
// e.hash=17 0001 0001
// newCap-1=31 0001 1111 newCap=32
// & =17 0001 0001 1+oldCap =
// 1+16
//元素在重新计算hash之后,因为n变为2倍,那么
//n-1的mask范围在高位多1bit(红色),因此新的
//index就会发生这样的变化:
// 0000 0001->0001 0001
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;
}
树形结构修剪
//参数介绍
//tab 表示保存桶头结点的哈希表
//index 表示从哪个位置开始修剪
//bit 要修剪的位数(哈希值)
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)
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);
}
}
}
“`
7822

被折叠的 条评论
为什么被折叠?



