HashMap.put()
2020-07-29 09:15:16 15 举报
JDK1.8 HashMap put()
作者其他创作
大纲/内容
x.parent = null;x.red = false;root = x;
d = (System.identityHashCode(a) <= System.identityHashCode(b) ? -1 : 1);
xp.red = false;
xppl.red = false;xp.red = false;xpp.red = true;x = xpp;
if ((pp = r.parent = p.parent) == null)
e.value = value;
rp.next = rn;
else if (!xp.red || (xpp = xp.parent) == null)
if (x == xp.right)
if (rp != null)
static final int hash(Object key)
hd.treeify(tab);
x.red = true;
(root = l).red = false;
p = e;
递归不变检查
pp.right = r;
int n;
pp.left = r;
else
if (xp != null)
if (p != null && (r = p.right) != null)
if ((tab = table) == null || (n = tab.length) == 0)
else if (p instanceof TreeNode)
e = p;
xp.left = x;
if (e != null)
void afterNodeInsertion(boolean evict) { }
if (x == xp.left)
(root = r).red = false;
break;
允许LinkedHashMap后处理的回调
if (root != null && tab != null && (n = tab.length) > 0)
afterNodeAccess(e);
root.next = first;root.prev = null;
if (tb != null && tb.next != t)if (tn != null && tn.prev != t)if (tp != null && t != tp.left && t != tp.right)if (tl != null && (tl.parent != t || tl.hash > t.hash))if (tr != null && (tr.parent != t || tr.hash < t.hash))if (t.red && tl != null && tl.red && tr != null && tr.red)if (tl != null && !checkInvariants(tl))if (tr != null && !checkInvariants(tr))
初始化或增加表大小
if ((rl = p.right = r.left) != null)
if (p != null && (l = p.left) != null)
调用hash函数生成哈希值(h = key.hashCode()) ^ (h >>> 16)
xp.right = x;
return true;
x.left = x.right = null;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
dir = 1;
if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
return oldValue;
if ((e = p.next) == null)
if ((pp = l.parent = p.parent) == null)
if (root == null)
return null;
K k = x.key;int h = x.hash; Class<?> kc = null;
if (root != first)
first.prev = root;
if ((ph = p.hash) > h)
r.left = p;p.parent = r;
int d;
if ((xp = x.parent) == null)
return root;
if (xp == (xppl = xpp.left))
if (dir <= 0)
HashMap
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
pp.right = l;
else if ((e = tab[index = (n - 1) & hash]) != null)
n = (tab = resize()).length;
xppr.red = false;xp.red = false;xpp.red = true;x = xpp;
x.red = false;return x;
if (first != null)
for (int binCount = 0; ; ++binCount)
++modCount;
else if (pp.right == p)
if ((p = tab[i = (n - 1) & hash]) == null)
if ((p = (dir <= 0) ? p.left : p.right) == null)
pp.left = l;
if (++size > threshold)
if (a == null || b == null || (d = a.getClass().getName().compareTo(b.getClass().getName())) == 0)
if ((tab[index] = hd) != null)
x.parent = xp;
resize();
return d;
if (xppl != null && xppl.red)
if (xpp != null)
l.right = p;p.parent = l;
if ((lr = p.left = l.right) != null)
afterNodeInsertion(evict);
return false;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
if ((xppr = xpp.right) != null && xppr.red)
assert checkInvariants(root);
rl.parent = p;
if (binCount >= TREEIFY_THRESHOLD - 1)
lr.parent = p;
V oldValue = e.value;
if ((rn = root.next) != null)
if (!onlyIfAbsent || oldValue == null)
else if (pp.left == p)
else if (ph < h)
0 条评论
回复 删除
下一页