前面分析了Set接口下的hashSet和linkedHashSet,下面接着來看treeSet,treeSet的底層實現是基於treeMap的。html
四個關注點在treeSet上的答案java
由於treeSet的底層是基於treeMap的,因此treeSet的數據結構就是treeMap的數據結構:紅黑樹,由於前面已經分析過了treeMap的數據結構,這裏再也不贅述。集合之TreeMap(含JDK1.8源碼分析)。數據結構
3.1 類的繼承關係函數
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable
說明:實現了NavigableSet接口,定義了一些共有的操做。源碼分析
3.2 類的屬性ui
/** * The backing map. */ private transient NavigableMap<E,Object> 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的操做,此處也是多態的概念。this
treeMap的繼承實現關係:spa
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable
而對於Object類型的屬性PRESENT,在分析hashSet的時候已經作了說明,由於map(treeSet中是NavigableMap)是存儲key-value鍵值對的,因此PRESENT只是配一下key-value中value的位置,起個佔位的做用,沒有什麼實際的意義,全部經過treeSet添加進來的key都對應同一個value值,PRESENT。code
3.3 類的構造函數orm
一、TreeSet(NavigableMap<E,Object> m)型
/** * Constructs a set backed by the specified navigable map. */ TreeSet(NavigableMap<E,Object> m) { this.m = m; }
說明:構建一個treeSet,基於navigable map(其實現類treeMap)實現的。
二、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 <i>mutually * comparable</i>: {@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<E,Object>()); }
說明:構建一個treeSet,排序是基於插入元素的天然順序。
三、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 <i>mutually * comparable</i> 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<? super E> comparator) { this(new TreeMap<>(comparator)); }
說明:構建一個treeSet,排序是基於自定義的比較器的排序規則。
四、TreeSet(Collection<? extends E> c)型
/** * Constructs a new tree set containing the elements in the specified * collection, sorted according to the <i>natural ordering</i> of its * elements. All elements inserted into the set must implement the * {@link Comparable} interface. Furthermore, all such elements must be * <i>mutually comparable</i>: {@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<? extends E> c) { this(); addAll(c); }
說明:構建一個treeSet,包含參數c中的元素,排序是基於元素的天然順序。
五、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<E> s) { this(s.comparator()); addAll(s); }
說明:構建一個treeSet,排序是基於SortedSet指定的排序規則。
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 * <tt>(e==null ? e2==null : e.equals(e2))</tt>. * 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方法。可參見:集合之TreeMap(含JDK1.8源碼分析)。
4.2 增:remove函數----刪除元素
/** * Removes the specified element from this set if it is present. * More formally, removes an element {@code e} such that * <tt>(o==null ? e==null : o.equals(e))</tt>, * 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方法。可參見:集合之TreeMap(含JDK1.8源碼分析)。
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 * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @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實現的,能夠自定義比較器對元素進行排序,或是使用元素的天然順序。