博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
集合之TreeSet(含JDK1.8源码分析)
阅读量:4880 次
发布时间:2019-06-11

本文共 7913 字,大约阅读时间需要 26 分钟。

一、前言

  前面分析了Set接口下的hashSet和linkedHashSet,下面接着来看treeSet,treeSet的底层实现是基于treeMap的。

  四个关注点在treeSet上的答案

二、treeSet的数据结构

  因为treeSet的底层是基于treeMap的,所以treeSet的数据结构就是treeMap的数据结构:红黑树,因为前面已经分析过了treeMap的数据结构,这里不再赘述。。

三、treeSet源码分析-属性及构造函数

  3.1 类的继承关系

public class TreeSet
extends AbstractSet
implements NavigableSet
, Cloneable, java.io.Serializable

  说明:实现了NavigableSet接口,定义了一些共有的操作。

  3.2 类的属性

/**     * The backing map.     */    private transient NavigableMap
m; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); //版本号 private static final long serialVersionUID = -2479143000061671589L;

  说明:属性m为NavigableMap接口,treeSet的一些操作都是基于此map的,而前面分析treeMap的时候,发现treeMap实现了NavigableMap接口,所以hashSet中基于NavigableMap接口的操作实际上都是基于其实现类treeMap的操作,此处也是多态的概念。

  treeMap的继承实现关系:

public class TreeMap
extends AbstractMap
implements NavigableMap
, Cloneable, java.io.Serializable

  而对于Object类型的属性PRESENT,在分析hashSet的时候已经做了说明,因为map(treeSet中是NavigableMap)是存储key-value键值对的,所以PRESENT只是配一下key-value中value的位置,起个占位的作用,没有什么实际的意义,所有通过treeSet添加进来的key都对应同一个value值,PRESENT。

  3.3 类的构造函数

  1、TreeSet(NavigableMap<E,Object> m)型

/**     * Constructs a set backed by the specified navigable map.     */    TreeSet(NavigableMap
m) { this.m = m; }

  说明:构建一个treeSet,基于navigable map(其实现类treeMap)实现的。

  2、TreeSet()型

/**     * Constructs a new, empty tree set, sorted according to the     * natural ordering of its elements.  All elements inserted into     * the set must implement the {
@link Comparable} interface. * Furthermore, all such elements must be mutually * comparable: {
@code e1.compareTo(e2)} must not throw a * {
@code ClassCastException} for any elements {
@code e1} and * {
@code e2} in the set. If the user attempts to add an element * to the set that violates this constraint (for example, the user * attempts to add a string element to a set whose elements are * integers), the {
@code add} call will throw a * {
@code ClassCastException}. */ public TreeSet() { this(new TreeMap
()); }

  说明:构建一个treeSet,排序是基于插入元素的自然顺序。

  3、TreeSet(Comparator<? super E> comparator)型

/**     * Constructs a new, empty tree set, sorted according to the specified     * comparator.  All elements inserted into the set must be mutually     * comparable by the specified comparator: {
@code comparator.compare(e1, * e2)} must not throw a {
@code ClassCastException} for any elements * {
@code e1} and {
@code e2} in the set. If the user attempts to add * an element to the set that violates this constraint, the * {
@code add} call will throw a {
@code ClassCastException}. * * @param comparator the comparator that will be used to order this set. * If {
@code null}, the {
@linkplain Comparable natural * ordering} of the elements will be used. */ public TreeSet(Comparator
comparator) { this(new TreeMap<>(comparator)); }

  说明:构建一个treeSet,排序是基于自定义的比较器的排序规则。

  4、TreeSet(Collection<? extends E> c)型

/**     * Constructs a new tree set containing the elements in the specified     * collection, sorted according to the natural ordering of its     * elements.  All elements inserted into the set must implement the     * {
@link Comparable} interface. Furthermore, all such elements must be * mutually comparable: {
@code e1.compareTo(e2)} must not throw a * {
@code ClassCastException} for any elements {
@code e1} and * {
@code e2} in the set. * * @param c collection whose elements will comprise the new set * @throws ClassCastException if the elements in {
@code c} are * not {
@link Comparable}, or are not mutually comparable * @throws NullPointerException if the specified collection is null */ public TreeSet(Collection
c) { this(); addAll(c); }

  说明:构建一个treeSet,包含参数c中的元素,排序是基于元素的自然顺序。

  5、TreeSet(SortedSet<E> s)型

/**     * Constructs a new tree set containing the same elements and     * using the same ordering as the specified sorted set.     *     * @param s sorted set whose elements will comprise the new set     * @throws NullPointerException if the specified sorted set is null     */    public TreeSet(SortedSet
s) { this(s.comparator()); addAll(s); }

  说明:构建一个treeSet,排序是基于SortedSet指定的排序规则。

四、treeSet源码分析-核心函数

  4.1 增:add函数----存储元素

/**     * Adds the specified element to this set if it is not already present.     * More formally, adds the specified element {
@code e} to this set if * the set contains no element {
@code e2} such that * (e==null ? e2==null : e.equals(e2)). * If this set already contains the element, the call leaves the set * unchanged and returns {
@code false}. * * @param e element to be added to this set * @return {
@code true} if this set did not already contain the specified * element * @throws ClassCastException if the specified object cannot be compared * with the elements currently in this set * @throws NullPointerException if the specified element is null * and this set uses natural ordering, or its comparator * does not permit null elements */ public boolean add(E e) { return m.put(e, PRESENT)==null; }

  说明:添加一个原先set中未存在的元素,返回true,若是该元素已经存在,set不做改变,返回false。

  可以看到其方法内部调用navigableMap的put方法,因为treeMap是其实现类,所以实际执行的时候,调用的是treeMap的put方法。可参见:。

  4.2 增:remove函数----删除元素

/**     * Removes the specified element from this set if it is present.     * More formally, removes an element {
@code e} such that * (o==null ? e==null : o.equals(e)), * if this set contains such an element. Returns {
@code true} if * this set contained the element (or equivalently, if this set * changed as a result of the call). (This set will not contain the * element once the call returns.) * * @param o object to be removed from this set, if present * @return {
@code true} if this set contained the specified element * @throws ClassCastException if the specified object cannot be compared * with the elements currently in this set * @throws NullPointerException if the specified element is null * and this set uses natural ordering, or its comparator * does not permit null elements */ public boolean remove(Object o) { return m.remove(o)==PRESENT; }

  说明:若set中存在要删除的元素,删除,返回true,不存在,返回false。

  可以看到其方法内部调用navigableMap的remove方法,因为treeMap是其实现类,所以实际执行的时候,调用的是treeMap的remove方法。可参见:。

  4.3 contains函数----是否存在该元素

/**     * Returns {
@code true} if this set contains the specified element. * More formally, returns {
@code true} if and only if this set * contains an element {
@code e} such that * (o==null ? e==null : o.equals(e)). * * @param o object to be checked for containment in this set * @return {
@code true} if this set contains the specified element * @throws ClassCastException if the specified object cannot be compared * with the elements currently in the set * @throws NullPointerException if the specified element is null * and this set uses natural ordering, or its comparator * does not permit null elements */ public boolean contains(Object o) { return m.containsKey(o); }

  说明:若set中存在该元素,返回true,不存在,返回false。

  可以看到其方法内部调用navigableMap的containsKey方法,因为treeMap是其实现类,所以实际执行的时候,调用的是treeMap的containsKey方法。

 五、总结

  总之,treeSet底层是基于treeMap实现的,可以自定义比较器对元素进行排序,或是使用元素的自然顺序。

转载于:https://www.cnblogs.com/zfyang2429/p/10457895.html

你可能感兴趣的文章
第二次冲刺作业
查看>>
【转】HTML, CSS和Javascript调试入门
查看>>
折线图-小案例
查看>>
STL:优先队列Priority Aueue
查看>>
蓝桥历年试题 套娃
查看>>
EF4.0和EF5.0增删改查的写法区别及执行Sql的方法
查看>>
作业一
查看>>
微信支付体验
查看>>
Excel导数据到数据库
查看>>
zz 悲催的程序员,以及程序员的悲催
查看>>
Thinkphp 3.2笔记
查看>>
RHEL7开机不能正常进入系统(图形化界面)
查看>>
Android开发环境搭建完全图解
查看>>
详解BOM头以及去掉BOM头的方法
查看>>
PHP 手机浏览器访问网站获取手机相关信息方法集锦
查看>>
09年电子竞赛参赛技巧经验11条(转载)
查看>>
CSS颜色
查看>>
前端自动化之(一)—浏览器自动实时刷新
查看>>
Unity 摄像头竖屏预览显示的问题
查看>>
HDU 5115 Dire Wolf(区间dp)
查看>>