当前位置:   article > 正文

JDK1.8源码解析-HashMap.TreeNode_treenode prev

treenode prev

在JDK 1.8 以前 HashMap 的实现是数组+链表,哈希函数很难保证元素的均匀分布。

当 HashMap 中有大量的元素都存放到同一个桶中时,这个桶下有一条长长的链表,这个时候 HashMap 就相当于一个单链表,假如单链表有 n 个元素,遍历的时间复杂度就是 O(n),完全失去了它的优势。

针对这种情况,JDK 1.8 中引入了 红黑树(查找时间复杂度为 O(logn))来优化这个问题。而红黑树的实现又交由内部类TreeNode实现。所以本文就hashmap中treenode的实现暂做分析。

treenode的类结构图

treenode继承了LinkedHashMap.Entry,使其结构具有键值对特征,而其自身又通过私有属性构造出红黑树结构(父节点、左子节点、右子节点、是否红色、前驱结点),其中prev表示该节点在链表中的前驱结点以便删除时确定位置。

  1. static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
  2. TreeNode<K,V> parent; // 构建红黑树需要的父节点
  3. TreeNode<K,V> left;
  4. TreeNode<K,V> right;
  5. TreeNode<K,V> prev; //需要在删除后取消链接
  6. boolean red;
  7. TreeNode(int hash, K key, V val, Node<K,V> next) {//构造函数
  8. super(hash, key, val, next);
  9. }

可以看到该节点既有红黑树的特点又有双向链表的特征(有前驱,有后继),这样做是为了方便bucket结构在链表与红黑树之间进行转换。及jdk1.8中可能(注意是可能,至于什么时候出现根据hashmap中的阈值决定什么时候bucket为tree什么时候为链表)出现的结构为:

方法

1.获取根节点的方法-root()

返回包含此节点所在的树的根节点

  1. final TreeNode<K,V> root() {
  2. for (TreeNode<K,V> r = this, p;;) {
  3. if ((p = r.parent) == null)//该节点的父节点为空则返回该节点
  4. return r;
  5. r = p;//父节点不为空则继续遍历
  6. }
  7. }

2.将根节点移动到bucket头部-moveRootToFront()

TreeNode在增加或删除节点后,都需要对整个树重新进行平衡,平衡之后的根节点也许就会发生变化,此时为了保证:如果HashMap元素数组根据下标取得的元素是一个TreeNode类型,那么这个TreeNode节点一定要是这颗树的根节点,同时也要是整个链表的首节点。

  1. /**
  2. * 把红黑树的根节点设为 其所在的数组槽 的第一个元素
  3. * 首先明确:TreeNode既是一个红黑树结构,也是一个双链表结构
  4. * 这个方法里做的事情,就是保证树的根节点一定也要成为链表的首节点
  5. */
  6. static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
  7. int n;
  8. if (root != null && tab != null && (n = tab.length) > 0) {
  9. int index = (n - 1) & root.hash; // 根据根节点的Hash值 和 HashMap的元素数组长度 取得根节点在数组中的位置
  10. TreeNode<K,V> first = (TreeNode<K,V>)tab[index]; // 首先取得该位置上的第一个节点对象
  11. if (root != first) {//如果根节点不是第一个结点
  12. Node<K,V> rn;
  13. tab[index] = root;//将结点集合中的相应位置换为根节点
  14. TreeNode<K,V> rp = root.prev;//记录根节点的前驱结点
  15. if ((rn = root.next) != null)//根节点的后继结点不为空
  16. ((TreeNode<K,V>)rn).prev = rp;//后继结点的前驱设为根节点的前驱
  17. if (rp != null)//前驱结点为空
  18. rp.next = rn;//前驱的后继
  19. if (first != null)
  20. first.prev = root;//第一个结点的前驱结点设为
  21. root.next = first;//根节点的后继为第一个结点
  22. root.prev = null;//前驱为空
  23. }
  24. // 在并发情况下进行递归运算时值是否会发生改变
  25. assert checkInvariants(root);
  26. }
  27. }
  28. /**
  29. *递归不变检查
  30. */
  31. static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
  32. TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
  33. tb = t.prev, tn = (TreeNode<K,V>)t.next;
  34. if (tb != null && tb.next != t)
  35. return false;
  36. if (tn != null && tn.prev != t)
  37. return false;
  38. if (tp != null && t != tp.left && t != tp.right)
  39. return false;
  40. if (tl != null && (tl.parent != t || tl.hash > t.hash))
  41. return false;
  42. if (tr != null && (tr.parent != t || tr.hash < t.hash))
  43. return false;
  44. if (t.red && tl != null && tl.red && tr != null && tr.red)
  45. return false;
  46. if (tl != null && !checkInvariants(tl))
  47. return false;
  48. if (tr != null && !checkInvariants(tr))
  49. return false;
  50. return true;
  51. }

3.在树中查找结点-find()

使用给定的hash和键查找从root p开始的树中的相应节点。 kc参数为键对象的class。对二叉树进行遍历,根据相应的hash值找到该键在二叉树中的位置,若查找到相应的位置,则在比较键值的大小,在比较时要注意该键对象是否实现了比较器,若实现了比较器,通过指定的CompareTo方法进行比较。

  1. final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
  2. TreeNode<K,V> p = this;//获取树的根节点
  3. do {
  4. int ph, dir; K pk;
  5. TreeNode<K,V> pl = p.left, pr = p.right, q;
  6. if ((ph = p.hash) > h)//当前hash值小于指定hash值,遍历左子树
  7. p = pl;
  8. else if (ph < h)//若大于指定hash值,遍历右子树
  9. p = pr;
  10. else if ((pk = p.key) == k || (k != null && k.equals(pk)))//若键相等
  11. return p;
  12. else if (pl == null)//若左子树为空,遍历右子树
  13. p = pr;
  14. else if (pr == null)//右子树为空,遍历左子树
  15. p = pl;
  16. else if ((kc != null ||
  17. (kc = comparableClassFor(k)) != null) &&
  18. (dir = compareComparables(kc, k, pk)) != 0)
  19. p = (dir < 0) ? pl : pr;
  20. else if ((q = pr.find(h, k, kc)) != null)//若以查找过,则直接返回
  21. return q;
  22. else
  23. p = pl;
  24. } while (p != null);//若结点不为空
  25. return null;
  26. }
  27. /**
  28. *如果它的形式为“class C implements Comparable <C>”,则返回x的Class,否则返回null。
  29. *判断对象是否实现了比较器
  30. */
  31. static Class<?> comparableClassFor(Object x) {
  32. if (x instanceof Comparable) {
  33. Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
  34. if ((c = x.getClass()) == String.class) // bypass checks
  35. return c;
  36. if ((ts = c.getGenericInterfaces()) != null) {
  37. for (int i = 0; i < ts.length; ++i) {
  38. if (((t = ts[i]) instanceof ParameterizedType) &&
  39. ((p = (ParameterizedType)t).getRawType() ==
  40. Comparable.class) &&
  41. (as = p.getActualTypeArguments()) != null &&
  42. as.length == 1 && as[0] == c) // type arg is c
  43. return c;
  44. }
  45. }
  46. }
  47. return null;
  48. }
  49. /**
  50. *如果x匹配kc(k的筛选可比类),则返回k.compareTo(x),否则返回0。
  51. */
  52. @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
  53. static int compareComparables(Class<?> kc, Object k, Object x) {
  54. return (x == null || x.getClass() != kc ? 0 :
  55. ((Comparable)k).compareTo(x));
  56. }

4.定义插入规则-tieBreakOrder()

定义插入规则。用于两个对象hashcode相等或者非比较的情况(该类没有实现比较器)下对两个对象的大小进行判断。若b的hashcode大于a的hashcode则放回1,否则返回-1.若两个类的类名不同或者两者都为空则返回0.

  1. static int tieBreakOrder(Object a, Object b) {
  2. int d;
  3. if (a == null || b == null ||
  4. (d = a.getClass().getName().
  5. compareTo(b.getClass().getName())) == 0)//类名相同,判断hashcode
  6. d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
  7. -1 : 1);
  8. return d;
  9. }

5.树与链表的转换-treeify&&untreeify

前面我们提到,bucket中的元素超过一定阈值时,链表会进行重构形成红黑树,而小于阈值时,红黑树又回变成链表。

我们可以画一张示意图:

可以看到,当我们进行查找时,元素通过红黑树进行查找并不会因为链表结构影响了查询效率,而且这样做还有一个好处就是根节点如果做链表用可以直接获取到整个链表,避免了重新遍历红黑树形成链表的复杂性。

  1. /**
  2. * 将当前结点及其后链接的结点形成树
  3. * @return root of tree
  4. */
  5. final void treeify(Node<K,V>[] tab) {
  6. TreeNode<K,V> root = null;
  7. for (TreeNode<K,V> x = this, next; x != null; x = next) {
  8. next = (TreeNode<K,V>)x.next;
  9. x.left = x.right = null;
  10. if (root == null) { //首次进入循环,确定头结点,为黑色
  11. x.parent = null;
  12. x.red = false;
  13. root = x;
  14. }
  15. else { //后面进入循环走的逻辑,x 指向树中的某个节点
  16. K k = x.key;//获取键
  17. int h = x.hash;//获取hash
  18. Class<?> kc = null;//获取键类型
  19. for (TreeNode<K,V> p = root;;) {
  20. int dir, ph;
  21. K pk = p.key;
  22. if ((ph = p.hash) > h)//根节点大于当前hash值
  23. dir = -1;
  24. else if (ph < h)
  25. dir = 1;
  26. else if ((kc == null &&
  27. (kc = comparableClassFor(k)) == null) ||
  28. (dir = compareComparables(kc, k, pk)) == 0)
  29. dir = tieBreakOrder(k, pk);
  30. TreeNode<K,V> xp = p;
  31. if ((p = (dir <= 0) ? p.left : p.right) == null) {
  32. x.parent = xp;
  33. if (dir <= 0)
  34. xp.left = x;
  35. else
  36. xp.right = x;
  37. root = balanceInsertion(root, x);
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. moveRootToFront(tab, root);
  44. }
  45. //返回一个非树列表,替换从该节点链接的列表。
  46. final Node<K,V> untreeify(HashMap<K,V> map) {
  47. Node<K,V> hd = null, tl = null;
  48. for (Node<K,V> q = this; q != null; q = q.next) {
  49. Node<K,V> p = map.replacementNode(q, null);
  50. if (tl == null)
  51. hd = p;
  52. else
  53. tl.next = p;
  54. tl = p;
  55. }
  56. return hd;
  57. }

6.树的左旋与右旋-rotateLeft&&rotateRight

左旋转示意图:

  1. //对root进行左旋转
  2. static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
  3. TreeNode<K,V> p) {
  4. TreeNode<K,V> r, pp, rl;
  5. if (p != null && (r = p.right) != null) {//r为p的右子树
  6. if ((rl = p.right = r.left) != null)//rl为p的右子节点的左子树,并将其设为p的右子树
  7. rl.parent = p;//修改后的父节点为p
  8. if ((pp = r.parent = p.parent) == null)//p的右子树的父节点变为p的父节点
  9. (root = r).red = false;//若p为根节点,则为黑色
  10. else if (pp.left == p)//若p为根节点的左子树
  11. pp.left = r;//则根节点的左子树为p的右子树
  12. else
  13. pp.right = r;//否则将其右子树设为p.right
  14. r.left = p;//右子树的左子树为p
  15. p.parent = r;//p的父节点为p的右子树
  16. }
  17. return root;
  18. }

右旋转示意图:

  1. //对root结点进行右旋转
  2. static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
  3. TreeNode<K,V> p) {
  4. TreeNode<K,V> l, pp, lr;
  5. if (p != null && (l = p.left) != null) {
  6. if ((lr = p.left = l.right) != null)//p的左子树为左子树的右子树
  7. lr.parent = p;
  8. if ((pp = l.parent = p.parent) == null)
  9. (root = l).red = false;
  10. else if (pp.right == p)
  11. pp.right = l;
  12. else
  13. pp.left = l;
  14. l.right = p;
  15. p.parent = l;
  16. }
  17. return root;
  18. }

7.插入删除元素后树的平衡--blanceInsertion&&blanceInsertion

插入和删除元素后也要通过左旋转与右旋转进行树的平衡,只不过这两个平衡方法中定义了左旋和右旋的条件(红黑树插入和删除元素的三种情况),即根据颜色判断左旋还是右旋。

  1. //插入元素后进行平衡
  2. static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
  3. TreeNode<K,V> x) {
  4. x.red = true;//将插入节点设为红色
  5. for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
  6. if ((xp = x.parent) == null) {//父节点为空,设为黑色
  7. x.red = false;
  8. return x;
  9. }
  10. else if (!xp.red || (xpp = xp.parent) == null)//父节点的父节点为空,返回根节点
  11. return root;
  12. if (xp == (xppl = xpp.left)) {//若“z的父节点”是“z的祖父节点的左孩子”
  13. if ((xppr = xpp.right) != null && xppr.red) {//case1:叔叔是红色
  14. xppr.red = false;//将“叔叔节点”设为黑色
  15. xp.red = false;//将“父节点”设为黑色
  16. xpp.red = true;//将“祖父节点”设为“红色”。
  17. x = xpp;//将“祖父节点”设为“当前节点”(红色节点)
  18. }
  19. else {
  20. if (x == xp.right) {//Case 2条件:叔叔是黑色,且当前节点是右孩子
  21. root = rotateLeft(root, x = xp);// 以“新的当前节点”为支点进行左旋。
  22. xpp = (xp = x.parent) == null ? null : xp.parent;// 将“父节点”作为“新的当前节点”
  23. }
  24. if (xp != null) {//Case 3条件:叔叔是黑色,且当前节点是左孩子。
  25. xp.red = false;
  26. if (xpp != null) {
  27. xpp.red = true;//将“祖父节点”设为“红色”
  28. root = rotateRight(root, xpp);//以“祖父节点”为支点进行右旋
  29. }
  30. }
  31. }
  32. }
  33. else {// 若“z的父节点”是“z的祖父节点的右孩子”,将上面的操作中“right”和“left”交换位置
  34. if (xppl != null && xppl.red) {
  35. xppl.red = false;
  36. xp.red = false;
  37. xpp.red = true;
  38. x = xpp;
  39. }
  40. else {
  41. if (x == xp.left) {
  42. root = rotateRight(root, x = xp);
  43. xpp = (xp = x.parent) == null ? null : xp.parent;
  44. }
  45. if (xp != null) {
  46. xp.red = false;
  47. if (xpp != null) {
  48. xpp.red = true;
  49. root = rotateLeft(root, xpp);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. //删除元素进行平衡
  57. static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
  58. TreeNode<K,V> x) {
  59. for (TreeNode<K,V> xp, xpl, xpr;;) {
  60. if (x == null || x == root)//若为根节点,直接返回
  61. return root;
  62. else if ((xp = x.parent) == null) {//若x的父节点为空
  63. x.red = false;//设为黑色
  64. return x;
  65. }
  66. else if (x.red) {//若为红色
  67. x.red = false;
  68. return root;
  69. }
  70. else if ((xpl = xp.left) == x) {// 若 “x”是“它父节点的左孩子”
  71. if ((xpr = xp.right) != null && xpr.red) {//Case 1: x是“黑+黑”节点,x的兄弟节点是红色
  72. xpr.red = false;// 将x的兄弟节点设为“黑色”
  73. xp.red = true;//将x的父节点设为“红色”。
  74. root = rotateLeft(root, xp);//对x的父节点进行左旋。
  75. xpr = (xp = x.parent) == null ? null : xp.right;//左旋后,重新设置x的兄弟节点。
  76. }
  77. if (xpr == null)
  78. x = xp;
  79. else {
  80. TreeNode<K,V> sl = xpr.left, sr = xpr.right;
  81. if ((sr == null || !sr.red) &&
  82. (sl == null || !sl.red)) {//Case 2: x是“黑+黑”节点,x的兄弟节点是黑色,x的兄弟节点的两个孩子都是黑色
  83. xpr.red = true;//将x的兄弟节点设为“红色”。
  84. x = xp;//设置“x的父节点”为“新的x节点”。
  85. }
  86. else {
  87. if (sr == null || !sr.red) {//Case 3: x是“黑+黑”节点,x的兄弟节点是黑色;x的兄弟节点的左孩子是红色,右孩子是黑色的
  88. if (sl != null)
  89. sl.red = false;//将x兄弟节点的左孩子设为“黑色”。
  90. xpr.red = true;//将x兄弟节点设为“红色”。
  91. root = rotateRight(root, xpr);//对x的兄弟节点进行右旋。
  92. xpr = (xp = x.parent) == null ?
  93. null : xp.right;
  94. }
  95. //Case 4:x是“黑+黑”节点,x的兄弟节点是黑色;x的兄弟节点的右孩子是红色的。(01) 将x父节点颜色 赋值给 x的兄弟节点。
  96. if (xpr != null) {
  97. xpr.red = (xp == null) ? false : xp.red;//将x兄弟节点的右子节设为“黑色”。
  98. if ((sr = xpr.right) != null)
  99. sr.red = false;
  100. }
  101. if (xp != null) {
  102. xp.red = false;//将x父节点设为“黑色”。
  103. root = rotateLeft(root, xp);//对x的父节点进行左旋。
  104. }
  105. x = root;
  106. }
  107. }
  108. }
  109. else { //若 “x”是“它父节点的右孩子”,将上面的操作中“right”和“left”交换位置,然后依次执行。
  110. if (xpl != null && xpl.red) {
  111. xpl.red = false;
  112. xp.red = true;
  113. root = rotateRight(root, xp);
  114. xpl = (xp = x.parent) == null ? null : xp.left;
  115. }
  116. if (xpl == null)
  117. x = xp;
  118. else {
  119. TreeNode<K,V> sl = xpl.left, sr = xpl.right;
  120. if ((sl == null || !sl.red) &&
  121. (sr == null || !sr.red)) {
  122. xpl.red = true;
  123. x = xp;
  124. }
  125. else {
  126. if (sl == null || !sl.red) {
  127. if (sr != null)
  128. sr.red = false;
  129. xpl.red = true;
  130. root = rotateLeft(root, xpl);
  131. xpl = (xp = x.parent) == null ?
  132. null : xp.left;
  133. }
  134. if (xpl != null) {
  135. xpl.red = (xp == null) ? false : xp.red;
  136. if ((sl = xpl.left) != null)
  137. sl.red = false;
  138. }
  139. if (xp != null) {
  140. xp.red = false;
  141. root = rotateRight(root, xp);
  142. }
  143. x = root;
  144. }
  145. }
  146. }
  147. }
  148. }

8.添加元素与删除结点-putTreeVal()

向红黑树中插入元素。首先根据键值对的hash值判断其是否存在,然后再将值插入到红黑树中,插入完毕后调用balanceInsertion()方法进行平衡插入操作。

删除红黑树中的元素时也要进行删除后树的平衡操作。

  1. final TreeNode putTreeVal(HashMap map, Node[] tab,
  2. int h, K k, V v) {
  3. Class kc = null;
  4. boolean searched = false;
  5. TreeNode root = (parent != null) ? root() : this;
  6. //每次添加元素时,从根节点遍历,对比哈希值
  7. for (TreeNode p = root;;) {
  8. int dir, ph; K pk;
  9. if ((ph = p.hash) > h)
  10. dir = -1;
  11. else if (ph < h)
  12. dir = 1;
  13. else if ((pk = p.key) == k || (k != null && k.equals(pk)))
  14. //如果当前节点的哈希值、键和要添加的都一致,就返回当前节点
  15. return p;
  16. else if ((kc == null &&
  17. (kc = comparableClassFor(k)) == null) ||
  18. (dir = compareComparables(kc, k, pk)) == 0) {
  19. //如果当前节点和要添加的节点哈希值相等,但是两个节点的键不是一个类,只好去挨个对比左右孩子
  20. if (!searched) {
  21. TreeNode q, ch;
  22. searched = true;
  23. if (((ch = p.left) != null &&
  24. (q = ch.find(h, k, kc)) != null) ||
  25. ((ch = p.right) != null &&
  26. (q = ch.find(h, k, kc)) != null))
  27. //如果从 ch 所在子树中可以找到要添加的节点,就直接返回
  28. return q;
  29. }
  30. //哈希值相等,但键无法比较,只好通过特殊的方法给个结果
  31. dir = tieBreakOrder(k, pk);
  32. }
  33. //经过前面的计算,得到了当前节点和要插入节点的一个大小关系
  34. //要插入的节点比当前节点小就插到左子树,大就插到右子树
  35. TreeNode xp = p;
  36. //这里有个判断,如果当前节点还没有左孩子或者右孩子时才能插入,否则就进入下一轮循环
  37. if ((p = (dir <= 0) ? p.left : p.right) == null) {
  38. Node xpn = xp.next;
  39. TreeNode x = map.newTreeNode(h, k, v, xpn);
  40. if (dir <= 0)
  41. xp.left = x;
  42. else
  43. xp.right = x;
  44. xp.next = x;
  45. x.parent = x.prev = xp;
  46. if (xpn != null)
  47. ((TreeNode)xpn).prev = x;
  48. //红黑树中,插入元素后必要的平衡调整操作
  49. moveRootToFront(tab, balanceInsertion(root, x));
  50. return null;
  51. }
  52. }
  53. }
  54. //删除树节点
  55. final void removeTreeNode(HashMap<K,V> map, Node<K,V>[] tab,
  56. boolean movable) {
  57. int n;
  58. if (tab == null || (n = tab.length) == 0)
  59. return;
  60. int index = (n - 1) & hash;//查找数组下标
  61. TreeNode<K,V> first = (TreeNode<K,V>)tab[index], root = first, rl;
  62. TreeNode<K,V> succ = (TreeNode<K,V>)next, pred = prev;
  63. if (pred == null)
  64. tab[index] = first = succ;
  65. else
  66. pred.next = succ;
  67. if (succ != null)
  68. succ.prev = pred;
  69. if (first == null)
  70. return;
  71. if (root.parent != null)
  72. root = root.root();//获取根节点
  73. if (root == null || root.right == null ||
  74. (rl = root.left) == null || rl.left == null) {
  75. tab[index] = first.untreeify(map); // 过小
  76. return;
  77. }
  78. TreeNode<K,V> p = this, pl = left, pr = right, replacement;
  79. if (pl != null && pr != null) {
  80. TreeNode<K,V> s = pr, sl;
  81. while ((sl = s.left) != null) // 成功找到该元素
  82. s = sl;
  83. boolean c = s.red; s.red = p.red; p.red = c; // 交换眼色
  84. TreeNode<K,V> sr = s.right;
  85. TreeNode<K,V> pp = p.parent;
  86. if (s == pr) { // p是s的直接父节点
  87. p.parent = s;
  88. s.right = p;
  89. }
  90. else {
  91. TreeNode<K,V> sp = s.parent;
  92. if ((p.parent = sp) != null) {
  93. if (s == sp.left)
  94. sp.left = p;
  95. else
  96. sp.right = p;
  97. }
  98. if ((s.right = pr) != null)
  99. pr.parent = s;
  100. }
  101. p.left = null;
  102. if ((p.right = sr) != null)
  103. sr.parent = p;
  104. if ((s.left = pl) != null)
  105. pl.parent = s;
  106. if ((s.parent = pp) == null)
  107. root = s;
  108. else if (p == pp.left)
  109. pp.left = s;
  110. else
  111. pp.right = s;
  112. if (sr != null)
  113. replacement = sr;
  114. else
  115. replacement = p;
  116. }
  117. else if (pl != null)
  118. replacement = pl;
  119. else if (pr != null)
  120. replacement = pr;
  121. else
  122. replacement = p;
  123. if (replacement != p) {
  124. TreeNode<K,V> pp = replacement.parent = p.parent;
  125. if (pp == null)
  126. root = replacement;
  127. else if (p == pp.left)
  128. pp.left = replacement;
  129. else
  130. pp.right = replacement;
  131. p.left = p.right = p.parent = null;
  132. }
  133. TreeNode<K,V> r = p.red ? root : balanceDeletion(root, replacement);
  134. if (replacement == p) { //
  135. TreeNode<K,V> pp = p.parent;
  136. p.parent = null;
  137. if (pp != null) {
  138. if (p == pp.left)
  139. pp.left = null;
  140. else if (p == pp.right)
  141. pp.right = null;
  142. }
  143. }
  144. if (movable)
  145. moveRootToFront(tab, r);
  146. }

9.树的分割--split

当扩容时,如果当前桶中元素结构是红黑树,并且元素个数小于链表还原阈值 UNTREEIFY_THRESHOLD (默认为 6),就会把桶中的树形结构缩小或者直接还原(切分)为链表结构。

  1. final void split(HashMap map, Node[] tab, int index, int bit) {
  2. TreeNode b = this;
  3. TreeNode loHead = null, loTail = null;
  4. TreeNode hiHead = null, hiTail = null;
  5. int lc = 0, hc = 0;
  6. for (TreeNode e = b, next; e != null; e = next) {
  7. next = (TreeNode)e.next;
  8. e.next = null;
  9. //如果当前节点哈希值的最后一位等于要修剪的 bit 值
  10. if ((e.hash & bit) == 0) {
  11. //就把当前节点放到 lXXX 树中
  12. if ((e.prev = loTail) == null)
  13. loHead = e;
  14. else
  15. loTail.next = e;
  16. //然后 loTail 记录 e
  17. loTail = e;
  18. //记录 lXXX 树的节点数量
  19. ++lc;
  20. }
  21. else { //如果当前节点哈希值最后一位不是要修剪的
  22. //就把当前节点放到 hXXX 树中
  23. if ((e.prev = hiTail) == null)
  24. hiHead = e;
  25. else
  26. hiTail.next = e;
  27. hiTail = e;
  28. //记录 hXXX 树的节点数量
  29. ++hc;
  30. }
  31. }
  32. if (loHead != null) {
  33. if (lc <= UNTREEIFY_THRESHOLD)//变为链表
  34. tab[index] = loHead.untreeify(map);
  35. else {
  36. //否则让索引位置的结点指向 lXXX 树,这个树被修剪过,元素少了
  37. tab[index] = loHead;
  38. if (hiHead != null) // (else is already treeified)
  39. loHead.treeify(tab);
  40. }
  41. }
  42. if (hiHead != null) {
  43. if (hc <= UNTREEIFY_THRESHOLD)
  44. tab[index + bit] = hiHead.untreeify(map);
  45. else {
  46. tab[index + bit] = hiHead;
  47. if (loHead != null)
  48. hiHead.treeify(tab);
  49. }
  50. }
  51. }

文章参考:Java 集合深入理解(17):HashMap 在 JDK 1.8 后新增的红黑树结构_张拭心的博客 shixinzhang-CSDN博客_hashmap1.8红黑树

                  红黑树的插入与删除_朱广健的博客-CSDN博客_红黑树删除时间复杂度

具体的treenode方法到此已经看完啦,这里只是bucket(bin)中的结构变化,具体hashmap实现看: JDK1.8源码解析-HashMap

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/596011
推荐阅读
相关标签
  

闽ICP备14008679号