java提升篇----TreeSet

1、TreeSet定义
咱们知道TreeMap是一个有序的二叉树,那么同理TreeSet一样也是一个有序的,它的做用是提供有序的Set集合。经过源码咱们知道TreeSet基础AbstractSet,实现NavigableSet、Cloneable、Serializable接口。其中AbstractSet提供 Set 接口的骨干实现,从而最大限度地减小了实现此接口所需的工做。NavigableSet是扩展的 SortedSet,具备了为给定搜索目标报告最接近匹配项的导航方法,这就意味着它支持一系列的导航方法。好比查找与指定目标最匹配项。Cloneable支持克隆,Serializable支持序列化。java

public class TreeSet<E> extends AbstractSet<E>app

implements NavigableSet<E>, Cloneable, java.io.Serializable

同时在TreeSet中定义了以下几个变量。this

private transient NavigableMap<E,Object> m;
//PRESENT会被当作Map的value与key构建成键值对spa

private static final Object PRESENT = new Object();code

其构造方法:排序

//默认构造方法,根据其元素的天然顺序进行排序
    public TreeSet() {
        this(new TreeMap<E,Object>());
    }
</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">构造一个包含指定 collection 元素的新 TreeSet,它按照其元素的天然顺序进行排序。</span>
<span style="color: rgb(0,0,255)">public</span> TreeSet(Comparator&lt;? <span style="color: rgb(0,0,255)">super</span> E&gt;<span style="color: rgb(0,0,0)"> comparator) {
        </span><span style="color: rgb(0,0,255)">this</span>(<span style="color: rgb(0,0,255)">new</span> TreeMap&lt;&gt;<span style="color: rgb(0,0,0)">(comparator));
}

</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">构造一个新的空 TreeSet,它根据指定比较器进行排序。</span>
<span style="color: rgb(0,0,255)">public</span> TreeSet(Collection&lt;? <span style="color: rgb(0,0,255)">extends</span> E&gt;<span style="color: rgb(0,0,0)"> c) {
    </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">();
    addAll(c);
}

</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">构造一个与指定有序 set 具备相同映射关系和相同排序的新 TreeSet。</span>
<span style="color: rgb(0,0,255)">public</span> TreeSet(SortedSet&lt;E&gt;<span style="color: rgb(0,0,0)"> s) {
    </span><span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">(s.comparator());
    addAll(s);
}

TreeSet(NavigableMap</span>&lt;E,Object&gt;<span style="color: rgb(0,0,0)"> m) {
    </span><span style="color: rgb(0,0,255)">this</span>.m =<span style="color: rgb(0,0,0)"> m;
}</span></pre>

2、TreeSet主要方法
一、add:将指定的元素添加到此 set(若是该元素还没有存在于 set 中)。接口

public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }

二、addAll:将指定 collection 中的全部元素添加到此 set 中。rem

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<? super E> cc = (Comparator<? super E>) 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);
    }

三、ceiling:返回此 set 中大于等于给定元素的最小元素;若是不存在这样的元素,则返回 null。get

public E ceiling(E e) {
        return m.ceilingKey(e);
    }

四、clear:移除此 set 中的全部元素。源码

public void clear() {
        m.clear();
    }

五、clone:返回 TreeSet 实例的浅表副本。属于浅拷贝。

public Object clone() {
        TreeSet<E> clone = null;
        try {
            clone = (TreeSet<E>) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    clone.m </span>= <span style="color: rgb(0,0,255)">new</span> TreeMap&lt;&gt;<span style="color: rgb(0,0,0)">(m);
    </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> clone;
}</span></pre>

六、comparator:返回对此 set 中的元素进行排序的比较器;若是此 set 使用其元素的天然顺序,则返回 null。

public Comparator<? super E> comparator() {
        return m.comparator();
    }

七、contains:若是此 set 包含指定的元素,则返回 true。

public boolean contains(Object o) {
        return m.containsKey(o);
    }

八、descendingIterator:返回在此 set 元素上按降序进行迭代的迭代器。

public Iterator<E> descendingIterator() {
        return m.descendingKeySet().iterator();
    }

九、descendingSet:返回此 set 中所包含元素的逆序视图。

public NavigableSet<E> descendingSet() {
        return new TreeSet<>(m.descendingMap());
    }

十、first:返回此 set 中当前第一个(最低)元素。

public E first() {
        return m.firstKey();
    }

十一、floor:返回此 set 中小于等于给定元素的最大元素;若是不存在这样的元素,则返回 null。

public E floor(E e) {
        return m.floorKey(e);
    }

十二、headSet:返回此 set 的部分视图,其元素严格小于 toElement。

public SortedSet<E> headSet(E toElement) {
        return headSet(toElement, false);
    }

1三、higher:返回此 set 中严格大于给定元素的最小元素;若是不存在这样的元素,则返回 null。

public E higher(E e) {
        return m.higherKey(e);
    }

1四、isEmpty:若是此 set 不包含任何元素,则返回 true。

public boolean isEmpty() {
        return m.isEmpty();
    }

1五、iterator:返回在此 set 中的元素上按升序进行迭代的迭代器。

public Iterator<E> iterator() {
        return m.navigableKeySet().iterator();
    }

1六、last:返回此 set 中当前最后一个(最高)元素。

public E last() {
        return m.lastKey();
    }

1七、lower:返回此 set 中严格小于给定元素的最大元素;若是不存在这样的元素,则返回 null。

public E lower(E e) {
        return m.lowerKey(e);
    }

1八、pollFirst:获取并移除第一个(最低)元素;若是此 set 为空,则返回 null。

public E pollFirst() {
        Map.Entry<E,?> e = m.pollFirstEntry();
        return (e == null) ? null : e.getKey();
    }

1九、pollLast:获取并移除最后一个(最高)元素;若是此 set 为空,则返回 null。

public E pollLast() {
        Map.Entry<E,?> e = m.pollLastEntry();
        return (e == null) ? null : e.getKey();
    }

20、remove:将指定的元素从 set 中移除(若是该元素存在于此 set 中)。

public boolean remove(Object o) {
        return m.remove(o)==PRESENT;
    }

2一、size:返回 set 中的元素数(set 的容量)。

public int size() {
        return m.size();
    }

2二、subSet:返回此 set 的部分视图

/**
     * 返回此 set 的部分视图,其元素范围从 fromElement 到 toElement。
     */
     public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
             E toElement,   boolean toInclusive) {
             return new TreeSet<>(m.subMap(fromElement, fromInclusive,
                  toElement,   toInclusive));
     }
 </span><span style="color: rgb(0,128,0)">/**</span><span style="color: rgb(0,128,0)">
  * 返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。
  </span><span style="color: rgb(0,128,0)">*/</span>
 <span style="color: rgb(0,0,255)">public</span> SortedSet&lt;E&gt;<span style="color: rgb(0,0,0)"> subSet(E fromElement, E toElement) {
     </span><span style="color: rgb(0,0,255)">return</span> subSet(fromElement, <span style="color: rgb(0,0,255)">true</span>, toElement, <span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">);
 }</span></pre>

2三、tailSet:返回此 set 的部分视图

/**
     * 返回此 set 的部分视图,其元素大于(或等于,若是 inclusive 为 true)fromElement。
     */
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
        return new TreeSet<>(m.tailMap(fromElement, inclusive));
    }
</span><span style="color: rgb(0,128,0)">/**</span><span style="color: rgb(0,128,0)">
 * 返回此 set 的部分视图,其元素大于等于 fromElement。
 </span><span style="color: rgb(0,128,0)">*/</span>
<span style="color: rgb(0,0,255)">public</span> SortedSet&lt;E&gt;<span style="color: rgb(0,0,0)"> tailSet(E fromElement) {
    </span><span style="color: rgb(0,0,255)">return</span> tailSet(fromElement, <span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">);
}</span></pre>

3、最后因为TreeSet是基于TreeMap实现的,因此若是咱们对treeMap有了必定的了解,对TreeSet那是小菜一碟,咱们从TreeSet中的源码能够看出,其实现过程很是简单,几乎全部的方法实现所有都是基于TreeMap的。

相关文章
相关标签/搜索