1. 程式人生 > >Java數據結構-HashMap

Java數據結構-HashMap

sta tree ret inf info ren lse .com alt

Java數據結構-HashMap

1. HashMap數據結構

沒有哈希沖突時,為數組,支持動態擴容

技術分享圖片

哈希沖突時,分為兩種情況:

1. 當沖突長度小於8或數組長度小於64(MIN_TREEIFY_CAPACITY默認值為64)時,為數組+鏈表(Node)

2. 當沖突長度大於8時,為數組+紅黑樹/鏈表(TreeNode)。

紅黑樹用於快速查找,鏈表用於遍歷。

技術分享圖片

2. 紅黑樹

HashMap中的TreeNode是紅黑樹的實現。

TreeNode幾個方法

1. 左旋轉

        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
                                              TreeNode
<K,V> p) { TreeNode<K,V> r, pp, rl; if (p != null && (r = p.right) != null) { if ((rl = p.right = r.left) != null) rl.parent = p; if ((pp = r.parent = p.parent) == null) (root = r).red = false
; else if (pp.left == p) pp.left = r; else pp.right = r; r.left = p; p.parent = r; } return root; }

實現效果如圖

技術分享圖片

2. 右旋轉

       static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
                                               TreeNode
<K,V> p) { TreeNode<K,V> l, pp, lr; if (p != null && (l = p.left) != null) { if ((lr = p.left = l.right) != null) lr.parent = p; if ((pp = l.parent = p.parent) == null) (root = l).red = false; else if (pp.right == p) pp.right = l; else pp.left = l; l.right = p; p.parent = l; } return root; }

實現效果如圖

技術分享圖片

3. 插入

 static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
                                                    TreeNode<K,V> x) {
            x.red = true;
            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
                if ((xp = x.parent) == null) {
                    x.red = false;
                    return x;
                }
                else if (!xp.red || (xpp = xp.parent) == null) //
                    return root;
                if (xp == (xppl = xpp.left)) {
                    if ((xppr = xpp.right) != null && xppr.red) { //
                        xppr.red = false;
                        xp.red = false;
                        xpp.red = true;
                        x = xpp;
                    }
                    else {
                        if (x == xp.right) {  //
                            root = rotateLeft(root, x = xp);
                            xpp = (xp = x.parent) == null ? null : xp.parent;
                        }
                        if (xp != null) {  //
                            xp.red = false;
                            if (xpp != null) {
                                xpp.red = true;
                                root = rotateRight(root, xpp);
                            }
                        }
                    }
                }
                else {
                    if (xppl != null && xppl.red) {  //
                        xppl.red = false;
                        xp.red = false;
                        xpp.red = true;
                        x = xpp;
                    }
                    else {
                        if (x == xp.left) {       //
                            root = rotateRight(root, x = xp);
                            xpp = (xp = x.parent) == null ? null : xp.parent;
                        }
                        if (xp != null) {   //
                            xp.red = false;
                            if (xpp != null) {
                                xpp.red = true;
                                root = rotateLeft(root, xpp);
                            }
                        }
                    }
                }
            }
        }

實現效果如下:

技術分享圖片

技術分享圖片

Java數據結構-HashMap