• TreeMap 原理


    基于jdk1.8

    TreeMap第一个想到的就是有序,当然也不是线程安全

    TreeMap实现NavigableMap接口,说明支持一系列的导航方法

    一、构造方法

        public TreeMap() {
            comparator = null;
        }
        public TreeMap(Comparator<? super K> comparator) {
            this.comparator = comparator;
        }
        public TreeMap(Map<? extends K, ? extends V> m) {
            comparator = null;
            putAll(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) {
            }
        }
        看到这4个构造方法就说明TreeMap是比较注重排序的,comparator即排序规则,不指定是按key类的自有排序规则进行排序后面具体看,其它按指定规则排序

    二、put方法
    通过put方法,我们就能看出基本数据结构和内存结构了,包括排序等等,这是一个信息量最大的方法

    public V put(K key, V value) {
            Entry<K,V> t = root;  //Entry类一看就知道了 红黑树
            if (t == null) {
                //这里是空树,比较同一个key看似没什么用,实际就一个用检查key是否为null,因此无论什么场景第一个put的key不能null
                compare(key, key); // type (and possibly null) check
    
                root = new Entry<>(key, value, null);//第一个节点设置root
                size = 1;
                modCount++;
                return null;
            }
            int cmp;
            Entry<K,V> parent;
            // split comparator and comparable paths
            Comparator<? super K> cpr = comparator;
            if (cpr != null) {    //有排序规则 这个if是按排序规则比较,左右挂节点
                do {
                    parent = t;
                    cmp = cpr.compare(key, t.key);    //注意能允许key为null的,只要指定的排序规则支持就可以
                    if (cmp < 0)
                        t = t.left;
                    else if (cmp > 0)
                        t = t.right;
                    else
                        return t.setValue(value);    //key相同直接覆盖
                } while (t != null);
            }
            else {    //无排序规则
                if (key == null)    //无排序规则时key不能为null
                    throw new NullPointerException();
                @SuppressWarnings("unchecked")
                    Comparable<? super K> k = (Comparable<? super K>) key;    //这里需要注意,没指定排序规则时key的类就必须实现Comparable排序接口,否则转换报错
                do {
                    parent = t;
                    cmp = k.compareTo(t.key);    //按key的实际类排序方案进行比较挂节点
                    if (cmp < 0)
                        t = t.left;
                    else if (cmp > 0)
                        t = t.right;
                    else
                        return t.setValue(value);
                } while (t != null);
            }
            //前面的if else找到了当前key需要挂的父节点,cmp决定了挂的左右
            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;
        }
        通过put方法知道,TreeMap数据结构只有红黑树,没有其它了,也不存在hash的问题

    三、get方法

    final Entry<K,V> getEntry(Object key) {
            // Offload comparator-based version for sake of performance
            if (comparator != null)    //有排序规则  详细看
                return getEntryUsingComparator(key);
            if (key == null)    //无排序规则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)
                    p = p.left;
                else if (cmp > 0)
                    p = p.right;
                else
                    return p;
            }
            return null;
        }
        
        //有排序规则时查找节点的方法  实际没什么区别,只是compare比较用了指定的排序规则而已
        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)
                        p = p.left;
                    else if (cmp > 0)
                        p = p.right;
                    else
                        return p;
                }
            }
            return null;
        }

    四、其它
    TreeMap整体看下来,主要就是红黑树的书法实现,其它没用到,至于remove等方法以及导航方法,和迭代器就不多说了。
    so 主要就是学习红黑树

  • 相关阅读:
    django如何给上传的图片重命名(给上传文件重命名)
    Nginx 常见问题解决
    nginx 出现413 Request Entity Too Large问题的解决方法
    python判断一个对象是否可迭代
    InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClas...
    Android Studio中新建和引用assets文件
    android 登录效果
    Android (争取做到)最全的底部导航栏实现方法
    抽屉效果的导航菜单
    Android开发之自定义局部导航菜单
  • 原文地址:https://www.cnblogs.com/zhaojj/p/7838255.html
Copyright © 2020-2023  润新知