TreeMap源码解析

JDK1.8TreeMap源码解析

/*
        TreeMap源码解析
        *重要属性
        private transient Entry<K,V> root;(根节点)
        private transient int size = 0; 长度
        * 空参构造器
        public TreeMap() {
            comparator = null;比较器赋值为null
        }
        * 带参构造器,需要传入一个外部比较器
        public TreeMap(Comparator<? super K> comparator) {
            this.comparator = comparator;
        }
        *
        * (内部节点类)
        static final class Entry<K,V> implements Map.Entry<K,V> {
            K key;存入的key
            V value; 存入的value
            Entry<K,V> left; 左节点
            Entry<K,V> right;右节点
            Entry<K,V> parent;父节点
            boolean color = BLACK;(红黑树颜色)
            * 构造器
            Entry(K key, V value, Entry<K,V> parent) {
                this.key = key;
                this.value = value;
                this.parent = parent;
            }
        */
        /*put方法源码解析
        public V put(K key, V value) {
            Entry<K,V> t = root;   t=null
            if (t == null) { (当二叉树没有任何元素的时候)
                compare(key, key);对key进行比较,忽略
                root = new Entry<>(key, value, null);   根节点->新子节点
                size = 1; 元素数量+1
                modCount++;
                return null; 返回null
            }
            //二叉树中包含元素
            int cmp;声明一个比较变量
            Entry<K,V> parent;声明一个父节点
            Comparator<? super K> cpr = comparator; 接收外部比较器
            if (cpr != null) { 使用内部比较器     相当于遍历整个二叉树
                do {
                    parent = t; 将父节点变量指向根节点
                    cmp = cpr.compare(key, t.key); 比较父节点的key与当前key,返回一个int变量
                    if (cmp < 0)
                        t = t.left;   根指针左移位
                    else if (cmp > 0)
                        t = t.right;  根指针右移位
                    else
                        return t.setValue(value);  key相同  覆盖value
                } while (t != null);当节点指针为null时停止遍历
            }
            else {  使用外部比较器
                if (key == null)   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;   结果小于0,根指针左移位
                    else if (cmp > 0) 结果大于0,根指针右移位
                        t = t.right;
                    else
                        return t.setValue(value);  相等时对value进行覆盖
                } while (t != null);遍历
            }
            Entry<K,V> e = new Entry<>(key, value, parent);将key:value封装为Entry对象,并使用parent指向父节点
            if (cmp < 0)     小于
                parent.left = e;  将父节点的左节点赋值为e
            else             大于
                parent.right = e; 将父节点的右节点赋值为e
            size++;          长度+1
            return null;     返 回null
        }
    */

版权声明:本文为qq_52751442原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。