探索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 有序不可重复

posted @ 2020-12-21 20:38  zliawk  阅读(316)  评论(0)    收藏  举报