1. 程式人生 > 實用技巧 >探索TreeMap底層實現

探索TreeMap底層實現

前言

探索TreeMap底層實現是基於JDK1.8,通過該類的註釋可以瞭解它的資料結構是紅黑樹,預設是按照自然順序進行排序(所有的鍵都必須去實現Comparable),當然也可以通過指定比較器進行排序(所有的鍵都必須實現Comparator)。至於什麼是紅黑樹,讀者最好有一定的瞭解,那看懂它的原始碼也就是分分鐘的事了。

資料結構


    //可序列化、克隆
    public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable {

        /**
         * 比較器
         * 通過該比較器來維持順序,若為null則按照自然順序
         */
        private final Comparator<? super K> comparator;

        /**
         * 紅黑樹的根節點
         */
        private transient Entry<K,V> root;

        /**
         * 紅黑樹中節點的個數
         */
        private transient int size = 0;

        /**
         * 結構修改的次數,用來檢測快速失敗
         */
        private transient int modCount = 0;

        /**
         * 紅色標識
         */
        private static final boolean RED   = false;

        /**
         * 黑色標識
         */
        private static final boolean BLACK = true;

        /**
         * 快取entrySet方法的返回值,用於迭代器
         */
        private transient EntrySet entrySet;

        /**
         * 儲存按照降序排列的鍵值對
         */
        private transient NavigableMap<K,V> descendingMap;

        /**
         * 儲存按照降序排列的鍵
         */
        private transient KeySet<K> navigableKeySet;
    }

建構函式


    /**
     * 預設初始化
     * 按照自然順序排列,所有的鍵都必須實現Comparable
     */
    public TreeMap() {
        comparator = null;
    }

    /**
     * 指定比較器來初始化
     * 按照比較器中定義的規則進行排列,所有的鍵都必須實現Comparator
     * @param comparator 比較器
     */
    public TreeMap(Comparator<? super K> comparator) {
        this.comparator = comparator;
    }

    /**
     * 新增指定集合到紅黑樹中
     * 按照自然順序排列,所有的鍵都必須實現Comparable
     * @param m 指定集合
     */
    public TreeMap(Map<? extends K, ? extends V> m) {
        comparator = null;
        putAll(m);
    }

    /** 
     * 新增指定有序集合到紅黑樹中,並採用該有序集合的比較器作為紅黑樹的比較器
     * @param m 有序集合
     */
    public TreeMap(SortedMap<K, ? extends V> m) {
        comparator = m.comparator();
        try {
            buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
        } catch (java.io.IOException cannotHappen) {
        } catch (ClassNotFoundException cannotHappen) {
        }
    }

簡單方法


    /**
     * 獲取紅黑樹中節點的個數
     * @return 節點的個數
     */
    public int size() {
        return size;
    }

    /**
     * 紅黑樹中是否包含指定鍵
     * @param key 指定鍵
     * @return 是否包含指定鍵
     */
    public boolean containsKey(Object key) {
        return getEntry(key) != null;
    }

    /**
     * 指定鍵獲取紅黑樹節點
     * @param key 指定鍵
     * @return 紅黑樹節點
     */
    final Entry<K,V> getEntry(Object key) {
        // Offload comparator-based version for sake of performance
        if (comparator != null)
            return getEntryUsingComparator(key);
        if (key == null)
            throw new NullPointerException();
        @SuppressWarnings("unchecked")
            Comparable<? super K> k = (Comparable<? super K>) key;
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = k.compareTo(p.key);
            if (cmp < 0) //比較結果小於0說明在左子樹上
                p = p.left;
            else if (cmp > 0) //比較結果大於0說明在右子樹上
                p = p.right;
            else
                return p;
        }
        return null;
    }

    /**
     * 在紅黑樹中通過指定鍵與指定比較器獲取節點
     * @param key 指定鍵
     */
    final Entry<K,V> getEntryUsingComparator(Object key) {
        @SuppressWarnings("unchecked")
            K k = (K) key;
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            Entry<K,V> p = root; //獲取紅黑樹的根節點
            while (p != null) {
                int cmp = cpr.compare(k, p.key);
                if (cmp < 0) //比較結果小於0說明在左子樹上
                    p = p.left;
                else if (cmp > 0)//比較結果大於0說明在右子樹上
                    p = p.right;
                else
                    return p;
            }
        }
        return null;
    }

    /**
     * 獲取等於或大於指定鍵的最小節點,若不存在,即紅黑樹中最大的鍵小於指定鍵則返回null
     * 大於指定鍵的最小節點:表示大於指定鍵且與指定鍵最為接近
     * @param key 指定鍵
     * @return null或指定鍵的最小節點
     */
    final Entry<K,V> getCeilingEntry(K key) {
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = compare(key, p.key);
            if (cmp < 0) { 
                if (p.left != null) //比較結果小於0可以明確的是已經找到大於指定鍵的節點了,但可能不是最小節點,故而往左子樹繼續查詢
                    p = p.left;
                else
                    return p;
            } else if (cmp > 0) {
                if (p.right != null) { //比較結果大於0說明還沒有找到大於指定鍵的節點,故而繼續往右子樹上找
                    p = p.right;
                } else {
                    /**
                     * 走到這裡是已經沒有右子樹可以查找了,說明已經沒有大於指定鍵的節點了,那麼我們需要嘗試往上查詢之前找到的大於指定鍵的最近節點或壓根就沒有大於指定鍵的節點
                     * 針對查詢大於指定鍵的節點,那麼指定鍵的節點一定在該節點的左子樹上(總體來看),所以最終只要不斷去查詢某個節點是否在其父節點的左子樹即可,若是則它的父節點就是大於指定鍵的最近節點,若最終
                     * parent == null說明整個樹始終都沒有大於指定鍵的節點,也就返回null
                     */
                    Entry<K,V> parent = p.parent;
                    Entry<K,V> ch = p;
                    while (parent != null && ch == parent.right) {
                        ch = parent;
                        parent = parent.parent;
                    }
                    return parent;
                }
            } else
                return p;
        }
        return null;
    }

    /**
     * 獲取等於或小於指定鍵的最大節點,若不存在,即紅黑樹中最小的鍵大於指定鍵則返回null
     * 小於指定鍵的最大節點:表示小於指定鍵且與指定鍵最為接近
     * @param key 指定鍵
     * @return null或指定鍵的最大節點
     */
    final Entry<K,V> getFloorEntry(K key) {
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = compare(key, p.key);
            if (cmp > 0) {
                if (p.right != null) //比較結果大於0可以明確的是已經找到小於指定鍵的節點了,但可能不是最大節點,故而往右子樹繼續查詢
                    p = p.right;
                else
                    return p;
            } else if (cmp < 0) {
                if (p.left != null) { //比較結果小於0說明還沒有找到小於指定鍵的節點,故而繼續往左子樹上找
                    p = p.left;
                } else {
                    /**
                     * 走到這裡是已經沒有左子樹可以查找了,說明已經沒有小於指定鍵的節點了,那麼我們需要嘗試往上查詢之前找到的小於指定鍵的最近節點或壓根就沒有小於指定鍵的節點
                     * 針對查詢小於指定鍵的節點,那麼指定鍵的節點一定在該節點的右子樹上(總體來看),所以最終只要不斷去查詢某個節點是否在其父節點的右子樹即可,若是則它的父節點就是小於指定鍵的最近節點,若最終
                     * parent == null說明整個樹始終都沒有小於指定鍵的節點,也就返回null
                     */
                    Entry<K,V> parent = p.parent;
                    Entry<K,V> ch = p;
                    while (parent != null && ch == parent.left) {
                        ch = parent;
                        parent = parent.parent;
                    }
                    return parent;
                }
            } else
                return p;

        }
        return null;
    }

    /**
     * 獲取大於指定鍵的最小節點,若不存在則返回null
     * @param key 指定鍵
     * @return null或指定鍵的最小節點
     */
    final Entry<K,V> getHigherEntry(K key) {
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = compare(key, p.key);
            if (cmp < 0) {
                if (p.left != null)
                    p = p.left;
                else
                    return p;
            } else {
                if (p.right != null) {
                    p = p.right;
                } else {
                    Entry<K,V> parent = p.parent;
                    Entry<K,V> ch = p;
                    while (parent != null && ch == parent.right) {
                        ch = parent;
                        parent = parent.parent;
                    }
                    return parent;
                }
            }
        }
        return null;
    }

    /**
     * 獲取小於指定鍵的最大節點,若不存在則返回null
     * lower < key
     * @param key 指定鍵
     * @return null或指定鍵的最大節點
     */
    final Entry<K,V> getLowerEntry(K key) {
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = compare(key, p.key);
            if (cmp > 0) {
                if (p.right != null)
                    p = p.right;
                else
                    return p;
            } else {
                if (p.left != null) {
                    p = p.left;
                } else {
                    Entry<K,V> parent = p.parent;
                    Entry<K,V> ch = p;
                    while (parent != null && ch == parent.left) {
                        ch = parent;
                        parent = parent.parent;
                    }
                    return parent;
                }
            }
        }
        return null;
    }

    /**
     * 新增節點後維持紅黑樹的平衡
     * @param x 新增節點
     */
    private void fixAfterInsertion(Entry<K,V> x) {
        x.color = RED;

        while (x != null && x != root && x.parent.color == RED) { //x的父節點是紅色
            if (parentOf(x) == leftOf(parentOf(parentOf(x)))) { //x的父節點在x的爺爺節點的左子樹上
                Entry<K,V> y = rightOf(parentOf(parentOf(x))); //x的叔叔節點
                if (colorOf(y) == RED) { //x的叔叔節點是紅色
                    setColor(parentOf(x), BLACK);
                    setColor(y, BLACK);
                    setColor(parentOf(parentOf(x)), RED);
                    x = parentOf(parentOf(x));
                } else { //x的叔叔節點是黑色
                    if (x == rightOf(parentOf(x))) { //x在x的父節點的右子樹上
                        x = parentOf(x);//x變成了x的父節點
                        rotateLeft(x);//將x的父節點進行左旋
                    }
                    //x在x的父節點的左子樹上
                    setColor(parentOf(x), BLACK);
                    setColor(parentOf(parentOf(x)), RED);
                    rotateRight(parentOf(parentOf(x)));
                }
            } else {//x的父節點在x的爺爺節點的右子樹上
                Entry<K,V> y = leftOf(parentOf(parentOf(x))); //x的叔叔節點
                if (colorOf(y) == RED) {
                    setColor(parentOf(x), BLACK);
                    setColor(y, BLACK);
                    setColor(parentOf(parentOf(x)), RED);
                    x = parentOf(parentOf(x));
                } else {
                    if (x == leftOf(parentOf(x))) { //x在x的父節點的左子樹上
                        x = parentOf(x);//x變成了x的父節點
                        rotateRight(x);//將x的父節點進行右旋
                    }
                    //x在x的父節點的右子樹上
                    setColor(parentOf(x), BLACK);
                    setColor(parentOf(parentOf(x)), RED);
                    rotateLeft(parentOf(parentOf(x)));
                }
            }
        }
        root.color = BLACK;
    }

    /**
     * 移除節點並調整紅黑樹使之平衡
     * @param p 移除節點
     */
    private void deleteEntry(Entry<K,V> p) {
        modCount++;
        size--;

        if (p.left != null && p.right != null) { //移除節點有兩個子節點
            Entry<K,V> s = successor(p); //查詢大於指定節點的最小節點
            p.key = s.key;
            p.value = s.value; //替換值
            p = s;
        } // p has 2 children

        Entry<K,V> replacement = (p.left != null ? p.left : p.right);

        if (replacement != null) { //移除節點至少有一個子節點,先更改移除節點的子節點與其父節點的關係
            replacement.parent = p.parent;
            if (p.parent == null)
                root = replacement;
            else if (p == p.parent.left)
                p.parent.left  = replacement;
            else
                p.parent.right = replacement;

            p.left = p.right = p.parent = null;

            if (p.color == BLACK) //移除節點為黑色的情況才會導致紅黑是失去平衡
                fixAfterDeletion(replacement);  //最後調整紅黑樹使之平衡
        } else if (p.parent == null) { //只有一個節點的情況下
            root = null;
        } else { //移除節點無子節點
            if (p.color == BLACK)
                fixAfterDeletion(p);

            if (p.parent != null) {
                if (p == p.parent.left)
                    p.parent.left = null;
                else if (p == p.parent.right)
                    p.parent.right = null;
                p.parent = null;
            }
        }
    }

    /**
     * 獲取大於指定節點的最小節點
     * 提供一篇文章:https://blog.csdn.net/iwts_24/article/details/87165743  講的內容不錯
     * @param t 指定節點
     * @return null或大於指定節點的最小節點
     */
    static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
        if (t == null)
            return null;
        else if (t.right != null) {
            Entry<K,V> p = t.right;
            /**
             * 在有右子樹的情況下查詢大於指定鍵的最小節點,所以應該查詢它的右子樹的最左邊的節點,因為越左邊說明越靠近指定鍵
             *
             *              5
             *            /   \
             *           3     6
             *         /  \
             *        1    4
             * 
             *       1   3 4 5 6
             * t = 3,則 p = 4
             */
            while (p.left != null)
                p = p.left;
            return p;
        } else {
            /**
             * 下面這段程式碼和上面的getCeilingEntry方法是一樣的
             * 在沒有右子樹的情況下往上查詢大於指定鍵的最小節點,即當某個節點為其父節點的左子樹時就是了
             *
             *              5
             *            /   \
             *           3     6
             *         /  \
             *        1    4
             * 
             *       1   3 4 5 6
             * t = 4,則 p = 5
             */
            Entry<K,V> p = t.parent;
            Entry<K,V> ch = t;
            while (p != null && ch == p.right) {
                ch = p;
                p = p.parent;
            }
            return p;
        }
    }

    /**
     * 移除節點後調整紅黑樹的平衡
     * 因為這裡涉及到紅黑樹的演算法,較為複雜,目前我也還沒有完全理解,雖然之前嘗試寫過一篇關於紅黑樹的文章,但發現還是有些問題,所以打算後面探索演算法的時候在更改
     * 由於咱們只是探索TreeMap的原始碼,所以這部分的內容個人覺得並不用花太多的時間去關注
     * @param x 移除節點或替代節點
     */
    private void fixAfterDeletion(Entry<K,V> x) {
        while (x != root && colorOf(x) == BLACK) {
            if (x == leftOf(parentOf(x))) {
                Entry<K,V> sib = rightOf(parentOf(x));

                if (colorOf(sib) == RED) {
                    setColor(sib, BLACK);
                    setColor(parentOf(x), RED);
                    rotateLeft(parentOf(x));
                    sib = rightOf(parentOf(x));
                }

                if (colorOf(leftOf(sib))  == BLACK &&
                    colorOf(rightOf(sib)) == BLACK) {
                    setColor(sib, RED);
                    x = parentOf(x);
                } else {
                    if (colorOf(rightOf(sib)) == BLACK) {
                        setColor(leftOf(sib), BLACK);
                        setColor(sib, RED);
                        rotateRight(sib);
                        sib = rightOf(parentOf(x));
                    }
                    setColor(sib, colorOf(parentOf(x)));
                    setColor(parentOf(x), BLACK);
                    setColor(rightOf(sib), BLACK);
                    rotateLeft(parentOf(x));
                    x = root;
                }
            } else { // symmetric
                Entry<K,V> sib = leftOf(parentOf(x));

                if (colorOf(sib) == RED) {
                    setColor(sib, BLACK);
                    setColor(parentOf(x), RED);
                    rotateRight(parentOf(x));
                    sib = leftOf(parentOf(x));
                }

                if (colorOf(rightOf(sib)) == BLACK &&
                    colorOf(leftOf(sib)) == BLACK) {
                    setColor(sib, RED);
                    x = parentOf(x);
                } else {
                    if (colorOf(leftOf(sib)) == BLACK) {
                        setColor(rightOf(sib), BLACK);
                        setColor(sib, RED);
                        rotateLeft(sib);
                        sib = leftOf(parentOf(x));
                    }
                    setColor(sib, colorOf(parentOf(x)));
                    setColor(parentOf(x), BLACK);
                    setColor(leftOf(sib), BLACK);
                    rotateRight(parentOf(x));
                    x = root;
                }
            }
        }

        setColor(x, BLACK);
    }

    /**
     * 清空
     */
    public void clear() {
        modCount++;
        size = 0;
        root = null;
    }

    /**
     * 淺拷貝
     * @return 克隆後的物件
     */
    public Object clone() {
        TreeMap<?,?> clone;
        try {
            clone = (TreeMap<?,?>) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e);
        }

        //將成員屬性恢復到初始值,預防後續兩個物件產生影響
        clone.root = null;
        clone.size = 0;
        clone.modCount = 0;
        clone.entrySet = null;
        clone.navigableKeySet = null;
        clone.descendingMap = null;

        //構建紅黑樹
        try {
            clone.buildFromSorted(size, entrySet().iterator(), null, null);
        } catch (java.io.IOException cannotHappen) {
        } catch (ClassNotFoundException cannotHappen) {
        }

        return clone;
    }

    /**
     * 指定鍵值對來構建一顆紅黑樹
     * @param size 紅黑樹的節點個數
     * @param it 包含鍵值對的迭代器
     * @param str 包含鍵值對的輸出流,it與str應該其中有一個不為空
     * @param defaultVal 若不為空,則使用該預設值作為鍵的值
     */
    private void buildFromSorted(int size, Iterator<?> it, java.io.ObjectInputStream str, V defaultVal)
        throws  java.io.IOException, ClassNotFoundException {
        this.size = size;
        root = buildFromSorted(0, 0, size-1, computeRedLevel(size), it, str, defaultVal);
    }

    /**
     * 計算節點的高度達到某個值時應該變成紅色,為了能夠更好的分配紅黑樹的顏色
     * 至於它是怎麼計算的,只能說這些寫演算法的人是真的牛逼
     * @param sz 紅黑樹的節點個數
     * @return 高度
     */
    private static int computeRedLevel(int sz) {
        int level = 0;
        for (int m = sz - 1; m >= 0; m = m / 2 - 1)
            level++;
        return level;
    }

    /**
     * 指定鍵值對來構建一顆紅黑樹
     * 註釋上說明了構建後的紅黑樹的顏色分配很鮮明,實際上最終是通過redLevel來確定哪一部分是黑色,哪一部分是紅色,但是順序依然還是原來的
     * @param level 當前節點的高度
     * @param lo 當前節點的子樹的第一個節點的索引
     * @param hi 子樹的最後一個節點的索引
     * @param redLevel 當前節點達到此高度時應該是紅色
     * @param it 包含鍵值對的迭代器
     * @param str 包含鍵值對的輸出流,it與str應該其中有一個不為空
     * @param defaultVal 若不為空,則使用該預設值作為鍵的值
     * @return 根節點,紅黑樹的結構可能會發生變化,但是它的順序不會變
     */
    @SuppressWarnings("unchecked")
    private final Entry<K,V> buildFromSorted(int level, int lo, int hi, int redLevel, Iterator<?> it, java.io.ObjectInputStream str, V defaultVal) throws  java.io.IOException, ClassNotFoundException {

        if (hi < lo) return null; //hi >= lo 說明子樹已經構造完成

        int mid = (lo + hi) >>> 1; //取中間位置,無符號右移相當於除以2

        Entry<K,V> left  = null;
        if (lo < mid) 
            /**
             * 通過遞迴的方式構建當前節點的左子樹
             * 若你瞭解紅黑樹的中序遍歷,那應該就很容易理解了
             * 
             *                   4
             *                 3    5
             *               2        6
             *             1            7
             *                           8
             * 中序遍歷:  1 2 3 4 5 6 7 8 9    索引是從0開始的,類似陣列
             * lo:0  hi:8 mid:4 redLevel:3  故當前節點是5,那麼要構建左子樹的話,可以認為5的左邊的那些數字都是它的左子樹中的節點,lo應該從0開始,hi應該是3
             */
            left = buildFromSorted(level+1, lo, mid - 1, redLevel, it, str, defaultVal);

        K key;
        V value;
        if (it != null) { //通過迭代器獲取鍵值對
            if (defaultVal==null) {
                Map.Entry<?,?> entry = (Map.Entry<?,?>)it.next();
                key = (K)entry.getKey();
                value = (V)entry.getValue();
            } else {
                key = (K)it.next();
                value = defaultVal;
            }
        } else { // 通過流的方式獲取鍵值對
            key = (K) str.readObject();
            value = (defaultVal != null ? defaultVal : (V) str.readObject());
        }

        Entry<K,V> middle =  new Entry<>(key, value, null);

        if (level == redLevel) //上面說了某個節點達到此高度時就要變成紅色
            middle.color = RED;

        if (left != null) { //左子樹構建完了就要關聯關係
            middle.left = left;
            left.parent = middle;
        }

        if (mid < hi) { //通過遞迴的方式構建當前節點的右子樹
            Entry<K,V> right = buildFromSorted(level+1, mid+1, hi, redLevel,
                                               it, str, defaultVal);
            middle.right = right;
            right.parent = middle;
        }

        return middle;
    }

    /**
     * 封裝鍵值對,以便控制某些方法不允許暴露給開發者
     * @param e 指定鍵值對
     * @return 封裝後的物件
     */
    static <K,V> Map.Entry<K,V> exportEntry(TreeMap.Entry<K,V> e) {
        return (e == null) ? null : new AbstractMap.SimpleImmutableEntry<>(e);
    }

    /**
     * 獲取包含所有鍵的集合
     * @return 包含所有鍵的Set集合 
     */
    public Set<K> keySet() {
        return navigableKeySet();
    }

    /**
     * 獲取包含所有鍵的集合
     * @return 包含所有鍵的Set集合 
     */
    public NavigableSet<K> navigableKeySet() {
        KeySet<K> nks = navigableKeySet;
        return (nks != null) ? nks : (navigableKeySet = new KeySet<>(this));
    }

    /**
     * 獲取包含按照降序排列的鍵的集合
     * @return 包含降序排列的鍵的集合
     */
    public NavigableSet<K> descendingKeySet() {
        return descendingMap().navigableKeySet();
    }

    /**
     * 獲取按照降序排列的集合
     * @return 降序排列的集合
     */
    public NavigableMap<K, V> descendingMap() {
        NavigableMap<K, V> km = descendingMap;
        return (km != null) ? km : (descendingMap = new DescendingSubMap<>(this, true, null, true, true, null, true));
    }

    /**
     * 獲取包含所有值的物件
     * @return 包含所有值的物件
     */
    public Collection<V> values() {
        Collection<V> vs = values;
        if (vs == null) {
            vs = new Values();
            values = vs;
        }
        return vs;
    }

    /**
     * 獲取包含所有鍵值對的集合
     * @return 包含所有鍵值對的集合
     */
    public Set<Map.Entry<K,V>> entrySet() {
        EntrySet es = entrySet;
        return (es != null) ? es : (entrySet = new EntrySet());
    }

    /**
     * 指定起始鍵與結束鍵及是否包含起始、結束鍵來獲取當前物件的子集
     * 當前物件是已經排好序了
     * 
     * TreeMap<Integer, String> treeMap = new TreeMap<>();
     * treeMap.put(5, "2");
     * treeMap.put(4, "2");
     * treeMap.put(10, "2");
     * treeMap.put(1, "2");
     * treeMap.put(20, "2");
     * treeMap.put(7, "2");
     * treeMap.put(8, "2");
     * 
     * System.out.println(treeMap.keySet()); -> [1, 4, 5, 7, 8, 10, 20]
     * System.out.println(treeMap.subMap(2,true, 6, true).keySet());  -> [4, 5]  實際上是在treeMap取2-6區間中的所有鍵 
     *
     * @param fromKey 起始鍵
     * @param fromInclusive 子集中是否包含起始鍵
     * @param toKey 結束鍵
     * @param toInclusive 子集中是否包含結束鍵
     * @return 子集物件
     */
    public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey,   boolean toInclusive) {
        return new AscendingSubMap<>(this, false, fromKey, fromInclusive, false, toKey, toInclusive);
    }

    /**
     * 指定結束鍵及是否包含結束鍵來獲取當前物件的子集
     * 當前物件是已經排好序了
     *
     * 參照上面的程式碼
     * System.out.println(treeMap.headMap(4, true).keySet()); -> [1, 4]
     *
     * @param toKey 結束鍵
     * @param inclusive 子集中是否包含結束鍵
     * @return 子集物件
     */
    public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
        return new AscendingSubMap<>(this, true,  null,  true, false, toKey, inclusive);
    }

    /**
     * 指定起始鍵及是否包含起始鍵來獲取當前物件的子集
     * @param fromKey 起始鍵
     * @param inclusive 子集中是否包含起始鍵
     * @return 子集物件
     */
    public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
        return new AscendingSubMap<>(this, false, fromKey, inclusive, true,  null, true);
    }

    /**
     * 指定起始鍵與結束鍵來獲取當前物件的子集
     * 包含起始鍵、不包含結束鍵
     * @param fromKey 起始鍵
     * @param toKey 結束鍵
     * @return 子集物件
     */
    public SortedMap<K,V> subMap(K fromKey, K toKey) {
        return subMap(fromKey, true, toKey, false);
    }

    /**
     * 指定結束鍵來獲取當前物件的子集
     * 不包含結束鍵
     * @param toKey 結束鍵
     * @return 子集物件
     */
    public SortedMap<K,V> headMap(K toKey) {
        return headMap(toKey, false);
    }

    /**
     * 指定起始鍵來獲取當前物件的子集
     * 包含起始鍵
     * @param fromKey 起始鍵
     * @return 子集物件
     */
    public SortedMap<K,V> tailMap(K fromKey) {
        return tailMap(fromKey, true);
    }

     /**
     * 替換指定鍵的節點的值,過程中要比較節點的值與指定值是否相等
     * @param key 指定鍵
     * @param oldValue 指定值
     * @param newValue 新值
     * @return 是否替換成功
     */
    @Override
    public boolean replace(K key, V oldValue, V newValue) {
        Entry<K,V> p = getEntry(key);
        if (p!=null && Objects.equals(oldValue, p.value)) {
            p.value = newValue;
            return true;
        }
        return false;
    }

    /**
     * 替換指定鍵的節點的值
     * @param key 指定鍵
     * @param value 新值
     * @return null或舊值
     */
    @Override
    public V replace(K key, V value) {
        Entry<K,V> p = getEntry(key);
        if (p!=null) {
            V oldValue = p.value;
            p.value = value;
            return oldValue;
        }
        return null;
    }

    /**
     * 按排列後的順序遍歷所有節點並執行指定動作
     * @param action 執行指定動作
     */
    @Override
    public void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        int expectedModCount = modCount;
        for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) { //successor是獲取大於當前節點的最小節點,按照紅黑樹的中序排列後,實際上就是獲取的下一個元素
            action.accept(e.key, e.value);

            if (expectedModCount != modCount) {
                throw new ConcurrentModificationException();
            }
        }
    }

    /**
     * 按排列後的順序遍歷所有節點並執行指定動作後獲取新值,利用新值替換所有節點的舊值
     * @param function 指定動作
     */
    @Override
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
        Objects.requireNonNull(function);
        int expectedModCount = modCount;

        for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
            e.value = function.apply(e.key, e.value);

            if (expectedModCount != modCount) {
                throw new ConcurrentModificationException();
            }
        }
    }

    /**
     * 比較兩個鍵
     * 若未指定比較器,則必須實現Comparable,通過覆寫該類來進行比較
     * 若是指定了比較器,則必須實現Comparator,通過覆寫該類來進行比較
     * @param k1 指定鍵
     * @param k2 指定鍵
     * @return 比較結果
     */
    final int compare(Object k1, Object k2) {
        return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2) : comparator.compare((K)k1, (K)k2);
    }

    /**
     * 比較兩個值
     * @param o1 指定值
     * @param o2 指定值
     * @return 比較結果
     */
    static final boolean valEquals(Object o1, Object o2) {
        return (o1==null ? o2==null : o1.equals(o2));
    }

    /**
     * 上面提到的關於獲取子集的內容就列出重要的方法,其餘的方法都是類似的,就不做重複性的工作了
     * 獲取子集時是已經按順序排列好了
     */
    abstract static class NavigableSubMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, java.io.Serializable {

        /**
         * 當前物件,也就是從該物件中獲取子集
         */
        final TreeMap<K,V> m;

        /**
         * 起始鍵與結束鍵
         */
        final K lo, hi;

        /**
         * fromStart為true的話表示當前物件的第一個鍵作為起始,相當於是已經明確了起始鍵,若為false就說明起始鍵需要呼叫者指定
         * toEnd為true的話表示當前物件的最後一個鍵作為結尾,相當於是已經明確了結束鍵,若為false就說明結束鍵需要呼叫者指定
         */
        final boolean fromStart, toEnd;

        /**
         * 子集是否需要包含起始鍵、結束鍵
         */
        final boolean loInclusive, hiInclusive;

        /**
         * 指定鍵是否小於起始鍵
         * 若等於則看loInclusive變數
         * @return 是否小於起始鍵
         */
        final boolean tooLow(Object key) {
            if (!fromStart) {
                int c = m.compare(key, lo);
                if (c < 0 || (c == 0 && !loInclusive))
                    return true;
            }
            return false;
        }

        /**
         * 指定鍵是否大於結束鍵
         * 若等於則看hiInclusive變數
         * @return 是否大於起始鍵
         */
        final boolean tooHigh(Object key) {
            if (!toEnd) {
                int c = m.compare(key, hi);
                if (c > 0 || (c == 0 && !hiInclusive))
                    return true;
            }
            return false;
        }

        /**
         * 指定鍵是否在起始鍵與結束鍵之間,該區間可能是[lo,hi]、(lo,hi) 、[lo,hi)、(lo,hi] 這取決於loInclusive、hiInclusive
         * @param key 指定鍵
         * @return 是否在起始鍵與結束鍵之間
         */
        final boolean inRange(Object key) {
            return !tooLow(key) && !tooHigh(key);
        }

        /**
         * 指定鍵是否在起始鍵與結束鍵之間,區間是[lo,hi]
         * @param key 指定鍵
         * @return 是否在起始鍵與結束鍵之間
         */
        final boolean inClosedRange(Object key) {
            return (fromStart || m.compare(key, lo) >= 0)
                && (toEnd || m.compare(hi, key) >= 0);
        }

        /**
         * 指定鍵是否在起始鍵與結束鍵之間
         * inclusive為true時,key在起始鍵與結束鍵之間不會報錯,不過若key剛好是等於起始鍵或結束鍵,那麼對應的區間必須是閉區間,假設key等於起始鍵,那麼它的區間應該是[lo,hi)或[lo,ih],若是等於結束鍵,那麼區間應該是(lo,hi]或[lo,hi]
         * inclusive為false時,key在起始鍵與結束鍵之間不會報錯,不過若key剛好是等於起始鍵或結束鍵,不管區間如何,都不會報錯
         * 當然了,不管inclusive的值如何,如果指定鍵大於結束鍵或小於起始鍵,那肯定會報錯
         * @param key 指定鍵
         * @param inclusive 子集中是否包含指定鍵 
         */
        final boolean inRange(Object key, boolean inclusive) {
            return inclusive ? inRange(key) : inClosedRange(key);
        }

        /**
         * 獲取當前物件中大於或等於起始鍵的節點
         * 因為指定了區間lo-hi,所以還要判斷該節點是否超過了結束鍵hi
         * 如果fromStart為true,說明已經指定了起始鍵,直接獲取第一個節點即可
         * @return null或大於或等於起始鍵的節點,為null說明要麼節點不存在,要麼該節點超過了結束鍵
         */
        final TreeMap.Entry<K,V> absLowest() {
            TreeMap.Entry<K,V> e = (fromStart ?  m.getFirstEntry() : (loInclusive ? m.getCeilingEntry(lo) : m.getHigherEntry(lo)));
            return (e == null || tooHigh(e.key)) ? null : e;
        }

        /**
         * 獲取當前物件中小於或等於結束鍵的節點
         * @return null或小於或等於結束鍵的節點,為null說明要麼節點不存在,要麼該節點小於起始鍵
         */
        final TreeMap.Entry<K,V> absHighest() {
            TreeMap.Entry<K,V> e = (toEnd ?  m.getLastEntry() : (hiInclusive ?  m.getFloorEntry(hi) : m.getLowerEntry(hi)));
            return (e == null || tooLow(e.key)) ? null : e;
        }

        /**
         * 獲取大於或等於指定鍵的節點
         * 若指定鍵小於起始鍵,那麼只需要獲取起始鍵即可
         * @param key 指定鍵
         * @return null或大於或等於指定鍵的節點
         */
        final TreeMap.Entry<K,V> absCeiling(K key) {
            if (tooLow(key))
                return absLowest();
            TreeMap.Entry<K,V> e = m.getCeilingEntry(key);
            return (e == null || tooHigh(e.key)) ? null : e;
        }

        /**
         * 獲取大於指定鍵的節點
         * @param key 指定鍵
         * @return null或大於指定鍵的節點
         */
        final TreeMap.Entry<K,V> absHigher(K key) {
            if (tooLow(key))
                return absLowest();
            TreeMap.Entry<K,V> e = m.getHigherEntry(key);
            return (e == null || tooHigh(e.key)) ? null : e;
        }

        /**
         * 獲取小於或等於指定鍵的節點
         * 若指定鍵大於結束鍵,則直接獲取結束鍵即可
         * @param key 指定鍵
         * @return null或小於或等於指定鍵的節點
         */
        final TreeMap.Entry<K,V> absFloor(K key) {
            if (tooHigh(key))
                return absHighest();
            TreeMap.Entry<K,V> e = m.getFloorEntry(key);
            return (e == null || tooLow(e.key)) ? null : e;
        }

        /**
         * 獲取小於指定鍵的節點
         * @param key 指定鍵
         * @return null或小於指定鍵的節點
         */
        final TreeMap.Entry<K,V> absLower(K key) {
            if (tooHigh(key))
                return absHighest();
            TreeMap.Entry<K,V> e = m.getLowerEntry(key);
            return (e == null || tooLow(e.key)) ? null : e;
        }

        /**
         * 獲取子集的尾節點(結果不包含尾節點)
         * 當hiInclusive為true時,說明結果要包含指定的結束鍵,所以它就取了比指定結束鍵還要大的節點作為尾節點
         * 當hiInclusive為false時,說明結果不包含指定的結束鍵,所以它就取了等於結束鍵的節點作為尾節點
         * 不管是哪一種,反正它是不包含尾節點,可以在迭代器的hasNext中看到:next != null && next.key != fenceKey;  說明不包含尾節點
         * @return 尾節點
         */
        final TreeMap.Entry<K,V> absHighFence() {
            return (toEnd ? null : (hiInclusive ? m.getHigherEntry(hi) : m.getCeilingEntry(hi)));
        }

        /**
         * 獲取子集的頭節點(不包含頭節點)
         * 分析同上
         * @return 頭節點
         */
        final TreeMap.Entry<K,V> absLowFence() {
            return (fromStart ? null : (loInclusive ? m.getLowerEntry(lo) : m.getFloorEntry(lo)));
        }

        
        //剩下的方法就不一一分析了,大多數都是類似的
    }


新增節點


    public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); //校驗指定鍵至是否實現Comparable或Comparator

            root = new Entry<>(key, value, null); //設定紅黑樹的根節點
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        Entry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) { //採用Comparator的方式進行比較,查詢新增節點應該放在哪個位置上
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value); //新增節點已經存在則進行替換值
            } while (t != null);
        }
        else { //採用Comparable的方式進行比較,查詢新增節點應該放在哪個位置上
            if (key == null)
                throw new NullPointerException();
            @SuppressWarnings("unchecked")
                Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        Entry<K,V> e = new Entry<>(key, value, parent); //構建節點並關聯關係
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }

移除節點


    /**
     * 移除指定鍵對應的節點
     * @param key 指定鍵
     * @return 移除節點的值
     */
    public V remove(Object key) {
        Entry<K,V> p = getEntry(key);
        if (p == null)
            return null;

        V oldValue = p.value;
        deleteEntry(p); //移除節點並調整紅黑樹的平衡
        return oldValue;
    }

    /**
     * 獲取排序後的第一個節點並移除
     * 獲取最左邊的節點並移除
     * @return 最左邊的節點
     */
    public Map.Entry<K,V> pollFirstEntry() {
        Entry<K,V> p = getFirstEntry();
        Map.Entry<K,V> result = exportEntry(p);
        if (p != null)
            deleteEntry(p);
        return result;
    }
    
    /**
     * 獲取排序後的最後一個節點並移除
     * 獲取最右邊的節點並移除
     * @return 最右邊的節點
     */
    public Map.Entry<K,V> pollLastEntry() {
        Entry<K,V> p = getLastEntry();
        Map.Entry<K,V> result = exportEntry(p);
        if (p != null)
            deleteEntry(p);
        return result;
    }


獲取節點


    /**
     * 獲取排序後的第一個節點
     * @return 排序後的第一個節點
     */
    public Map.Entry<K,V> firstEntry() {
        return exportEntry(getFirstEntry());
    }

    /**
     * 獲取最左邊的節點,實際上就是在獲取排序後的第一個節點
     * @return 最左邊的節點
     */
    final Entry<K,V> getFirstEntry() {
        Entry<K,V> p = root;
        if (p != null)
            while (p.left != null)
                p = p.left;
        return p;
    }

    /**
     * 獲取排序後的最後一個節點
     * @return 排序後的最後一個節點
     */
    public Map.Entry<K,V> lastEntry() {
        return exportEntry(getLastEntry());
    }

    /**
     * 獲取最右邊的節點,實際上就是在獲取排序後的最後一個節點
     * @return 最右邊的節點
     */
    final Entry<K,V> getLastEntry() {
        Entry<K,V> p = root;
        if (p != null)
            while (p.right != null)
                p = p.right;
        return p;
    }

    /**
     * 獲取小於指定鍵的最大節點
     * @param key 指定鍵
     * @return 小於指定鍵的最大節點
     */
    public Map.Entry<K,V> lowerEntry(K key) {
        return exportEntry(getLowerEntry(key));
    }

    /**
     * 獲取小於指定鍵的最大節點的鍵
     * @param key 指定鍵
     * @return 小於指定鍵的最大節點的鍵
     */
    public K lowerKey(K key) {
        return keyOrNull(getLowerEntry(key));
    }

    /**
     * 獲取等於或小於指定鍵的最大節點
     * @param key 指定鍵
     * @return 等於或小於指定鍵的最大節點
     */
    public Map.Entry<K,V> floorEntry(K key) {
        return exportEntry(getFloorEntry(key));
    }

    /**
     * 獲取小於指定鍵的最大節點的鍵
     * @param key 指定鍵
     * @return 小於指定鍵的最大節點的鍵
     */
    public K floorKey(K key) {
        return keyOrNull(getFloorEntry(key));
    }

    /**
     * 獲取等於或大於指定鍵的最小節點
     * @param key 指定鍵
     * @return 等於或大於指定鍵的最小節點
     */
    public Map.Entry<K,V> ceilingEntry(K key) {
        return exportEntry(getCeilingEntry(key));
    }

     /**
     * 獲取等於或大於指定鍵的最小節點的鍵
     * @param key 指定鍵
     * @return 等於或大於指定鍵的最小節點的鍵
     */
    public K ceilingKey(K key) {
        return keyOrNull(getCeilingEntry(key));
    }

    /**
     * 獲取大於指定鍵的最小節點
     * @param key 指定鍵
     * @return 大於指定鍵的最小節點
     */
    public Map.Entry<K,V> higherEntry(K key) {
        return exportEntry(getHigherEntry(key));
    }

    /**
     * 獲取大於指定鍵的最小節點的鍵
     * @param key 指定鍵
     * @return 大於指定鍵的最小節點的鍵
     */
    public K higherKey(K key) {
        return keyOrNull(getHigherEntry(key));
    }

  • 若要按自然順序排列則鍵必須實現Comparable,此時TreeMap中鍵不可以為null;若要自定義排列順序則鍵必須實現Comparator,此時TreeMap中鍵可以為null。

  • TreeMap中的很多演算法跟紅黑樹中的中序有很大的關聯,所以最好提前瞭解。

  • TreeMap有序不可重複,非執行緒安全。

  • TreeMap的資料結構是紅黑樹

重點關注

紅黑樹 Comparable與Comparator 有序不可重複