首页 > 其他 > 详细

探索TreeSet底层实现

时间:2020-12-21 21:09:46      阅读:34      评论:0      收藏:0      [点我收藏+]

前言

TreeSet的内部实现基于TreeMap,所以它的数据结构是红黑树。注释也不总结了,此探索是基于JDK1.8,直接进入正题。

数据结构


    public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable {
        
        //存储元素
        private transient NavigableMap<E,Object> m;

        //既然用了TreeMap就要考虑值应该存什么,就是它了,不管新增的元素是什么,它都作为值
        private static final Object PRESENT = new Object();

    }

构造函数


    /**
     * 指定NavigableMap实现类来初始化
     * ConcurrentSkipListMap是NavigableMap的实现类!!!埋下伏笔
     * @param m 指定实现类
     */
    TreeSet(NavigableMap<E,Object> m) {
        this.m = m;
    }

    /**
     * 默认初始化
     */
    public TreeSet() {
        this(new TreeMap<E,Object>());
    }

    /**
     * 指定比较器进行初始化
     * @param comparator 比较器
     */
    public TreeSet(Comparator<? super E> comparator) {
        this(new TreeMap<>(comparator));
    }

    /**
     * 添加指定集合进行初始化
     * @param c 指定集合
     */
    public TreeSet(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    /**
     * 指定Set集合进行初始化
     * @param s Set集合
     */
    public TreeSet(SortedSet<E> s) {
        this(s.comparator());
        addAll(s);
    }

简单方法


    /**
     * 获取迭代器
     * @return 迭代器
     */
    public Iterator<E> iterator() {
        return m.navigableKeySet().iterator();
    }

    /**
     * 获取按降序排列的迭代器
     * @return 降序排列的迭代器
     */
    public Iterator<E> descendingIterator() {
        return m.descendingKeySet().iterator();
    }

    /**
     * 获取按降序排列的Set集合
     * @return 降序排列的Set集合
     */
    public NavigableSet<E> descendingSet() {
        return new TreeSet<>(m.descendingMap());
    }

    /**
     * TreeSet集合的长度
     * @return 元素个数
     */
    public int size() {
        return m.size();
    }

    /**
     * TreeSet集合是否为空
     * @return 是否为空
     */
    public boolean isEmpty() {
        return m.isEmpty();
    }

    /**
     * TreeSet是否包含指定元素
     * @param o 指定元素
     * @return 是否包含指定元素
     */
    public boolean contains(Object o) {
        return m.containsKey(o);
    }

    /**
     * 新增元素
     * @param e 指定元素
     * @return 是否新增成功
     */
    public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }

    /**
     * 移除指定元素
     * @param o 指定元素
     * @return 是否移除成功
     */
    public boolean remove(Object o) {
        return m.remove(o)==PRESENT;
    }

    /**
     * 清空
     */
    public void clear() {
        m.clear();
    }

    /**
     * 批量添加指定集合
     * @param c 指定集合
     * @return 是否添加成功
     */
    public  boolean addAll(Collection<? extends E> c) {
        // Use linear-time version if applicable
        if (m.size()==0 && c.size() > 0 &&
            c instanceof SortedSet &&
            m instanceof TreeMap) {
            SortedSet<? extends E> set = (SortedSet<? extends E>) c;
            TreeMap<E,Object> map = (TreeMap<E, Object>) m;
            Comparator<?> cc = set.comparator();
            Comparator<? super E> mc = map.comparator();
            if (cc==mc || (cc != null && cc.equals(mc))) {
                map.addAllForTreeSet(set, PRESENT); //指定集合来添加一颗红黑树
                return true;
            }
        }
        return super.addAll(c);
    }

    /**
     * 指定起始元素与结束元素及是否包含起始、结束元素来获取当前对象的子集
     * 当前对象是已经排好序了
     * @param fromElement 起始元素
     * @param fromInclusive 子集中是否包含起始元素
     * @param toElement 结束元素
     * @param toInclusive 子集中是否包含结束元素
     * @return 子集对象
     */
    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement,   boolean toInclusive) {
        return new TreeSet<>(m.subMap(fromElement, fromInclusive, toElement, toInclusive));
    }

    /**
     * 指定结束元素及是否包含结束元素来获取当前对象的子集
     * 当前对象是已经排好序了,相当于起始元素已经指定好了
     * 
     * @param toElement 结束元素
     * @param inclusive 子集中是否包含结束元素
     * @return 子集对象
     */
    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
        return new TreeSet<>(m.headMap(toElement, inclusive));
    }

    /**
     * 指定起始元素及是否包含起始元素来获取当前对象的子集
     * 相当于介绍元素已经指定好了
     * @param fromElement 起始元素
     * @param inclusive 子集中是否包含起始元素
     * @return 子集对象
     */
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
        return new TreeSet<>(m.tailMap(fromElement, inclusive));
    }

    /**
     * 指定起始元素与结束元素来获取当前对象的子集
     * 包含起始元素、不包含结束元素
     * @param fromElement 起始元素
     * @param toElement 结束元素
     * @return 子集对象
     */
    public SortedSet<E> subSet(E fromElement, E toElement) {
        return subSet(fromElement, true, toElement, false);
    }

    /**
     * 获取比较器
     * @return 比较器
     */
    public Comparator<? super E> comparator() {
        return m.comparator();
    }

    /**
     * 获取排序后的第一个元素
     * @return 排序后的第一个元素
     */
    public E first() {
        return m.firstKey();
    }

    /**
     * 获取排序后的最后一个元素
     * @return 排序后的最后一个元素
     */
    public E last() {
        return m.lastKey();
    }

    /**
     * 获取小于指定元素的最大值
     * @param key 指定元素
     * @return 小于指定元素的最大值
     */
    public E lower(E e) {
        return m.lowerKey(e);
    }

    /**
     * 获取等于或小于指定元素的最大值
     * @param key 指定元素
     * @return 等于或小于指定元素的最大值
     */
    public E floor(E e) {
        return m.floorKey(e);
    }

    /**
     * 获取等于或大于指定元素的最小值
     * @param key 指定元素
     * @return 等于或大于指定元素的最小值
     */
    public E ceiling(E e) {
        return m.ceilingKey(e);
    }

    /**
     * 获取大于指定元素的最小值
     * @param key 指定元素
     * @return 大于指定元素的最小值
     */
    public E higher(E e) {
        return m.higherKey(e);
    }

    /**
     * 获取排序后的第一个元素并移除
     * 获取最左边的元素并移除
     * @return 最左边的元素
     */
    public E pollFirst() {
        Map.Entry<E,?> e = m.pollFirstEntry();
        return (e == null) ? null : e.getKey();
    }

    /**
     * 获取排序后的最后一个元素并移除
     * 获取最右边的元素并移除
     * @return 最右边的元素
     */
    public E pollLast() {
        Map.Entry<E,?> e = m.pollLastEntry();
        return (e == null) ? null : e.getKey();
    }

总结

  • TreeSet底层是TreeMa,所以它的数据结构是红黑树。

  • TreeSet有序、不可重复、非线程安全。

  • TreeSet默认按照自然顺序排列元素,可指定比较器来自定义排序。

重点关注

基于TreeMap

探索TreeSet底层实现

原文:https://www.cnblogs.com/zlia/p/14165345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!