Java-Collection源码分析(十四)——SortedSet、NavigableSet和TreeSet

来源:互联网 发布:预防网络诈骗班会流程 编辑:程序博客网 时间:2024/06/09 18:52

一、SortedSet接口

此接口主要定义了一些抽象的基本方法

public interface SortedSet<E> extends Set<E> {    //返回用于对该集合中的元素进行排序的比较器,如果此集合使用其元素的自然排序,则返回null。    Comparator<? super E> comparator();    //返回此集合的部分的视图,其元素的范围从fromElement(包括)到toElement,排除。    SortedSet<E> subSet(E fromElement, E toElement);    //返回该集合的部分的视图,该部分的元素严格小于toElement。    SortedSet<E> headSet(E toElement);    //返回该集合的元素大于或等于fromElement的部分的视图。    SortedSet<E> tailSet(E fromElement);    //返回此集合中当前的第一个(最低)元素。    E first();    //返回此集合中当前的最后(最高)元素。    E last();    //在此排序集中的元素上创建一个Spliterator。    default Spliterator<E> spliterator() {        return new Spliterators.IteratorSpliterator<E>(                this, Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED) {            @Override            public Comparator<? super E> getComparator() {                return SortedSet.this.comparator();            }        };    }}

二、NavigableSet

此接口继承了SortedSet接口,并在此基础上又定义了一些其他的抽象方法
public interface NavigableSet<E> extends SortedSet<E> {    //返回该集合中最大的元素严格小于给定的元素,如果没有这样的元素,则返回null。    E lower(E e);    //返回该集合中最大的元素小于或等于给定的元素,如果没有这样的元素,返回null。    E floor(E e);    //返回此集合中最小元素大于或等于给定元素,如果没有此元素,则返回null。    E ceiling(E e);    //返回严格大于给定元素的此集合中的最小元素,如果没有此元素则返回null。    E higher(E e);    //检索并删除第一个(最低)元素,如果此集合为空,则返回null。    E pollFirst();    //检索并删除最后一个(最高)元素,如果此集合为空,则返回null。    E pollLast();    //以升序方式返回该集合中的元素的迭代器。    Iterator<E> iterator();    //返回此集合中包含的元素的反向排序视图。    NavigableSet<E> descendingSet();    //以降序返回该集合中的元素的迭代器。    Iterator<E> descendingIterator();    //返回该集合的部分的视图,其元素的范围从fromElement到toElement。    NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement,   boolean toInclusive);    //返回元素小于(或等于,包含值为true)toElement的该集合的部分视图    NavigableSet<E> headSet(E toElement, boolean inclusive);    //返回此集合的部分的视图,该部分的元素大于(或等于,如果inclusive为true)fromElement。    NavigableSet<E> tailSet(E fromElement, boolean inclusive);    //返回此集合的部分的视图,其元素的范围从fromElement(包括)到toElement,排除。    SortedSet<E> subSet(E fromElement, E toElement);    //返回该集合的部分的视图,该部分的元素严格小于toElement。    SortedSet<E> headSet(E toElement);    //返回该集合的元素大于或等于fromElement的部分的视图。    SortedSet<E> tailSet(E fromElement);}
三、TreeSet
这个类在实现方法过程中主要是调用了NavigableMap和TreeMap中的方法,来实现红黑树结构功能和该类自己定义的一些方法。
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable{    private transient NavigableMap<E,Object> m;    private static final Object PRESENT = new Object();    TreeSet(NavigableMap<E,Object> m) {        this.m = m;    }    public TreeSet() {        this(new TreeMap<E,Object>());    }    public TreeSet(Comparator<? super E> comparator) {        this(new TreeMap<>(comparator));    }    public TreeSet(Collection<? extends E> c) {        this();        addAll(c);    }    public TreeSet(SortedSet<E> s) {        this(s.comparator());        addAll(s);    }    public Iterator<E> iterator() {        return m.navigableKeySet().iterator();    }    public Iterator<E> descendingIterator() {        return m.descendingKeySet().iterator();    }    public NavigableSet<E> descendingSet() {        return new TreeSet<>(m.descendingMap());    }    //返回此集合中的元素数(其基数)。    public int size() {        return m.size();    }    //如果此集合不包含元素,则返回true。    public boolean isEmpty() {        return m.isEmpty();    }    //如果此集合包含指定的元素,则返回true。    public boolean contains(Object o) {        return m.containsKey(o);    }    //将指定的元素添加到此集合(如果尚未存在)。    public boolean add(E e) {        return m.put(e, PRESENT)==null;    }    //如果存在,则从该集合中删除指定的元素。    public boolean remove(Object o) {        return m.remove(o)==PRESENT;    }    //从该集合中删除所有元素。    public void clear() {        m.clear();    }    //将指定集合中的所有元素添加到此集合中。    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);    }    //返回该集合的部分的视图,其元素的范围从fromElement到toElement。    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,                                  E toElement,   boolean toInclusive) {        return new TreeSet<>(m.subMap(fromElement, fromInclusive,                                       toElement,   toInclusive));    }    //返回元素小于(或等于,包含值为true)toElement的该集合的部分视图。    public NavigableSet<E> headSet(E toElement, boolean inclusive) {        return new TreeSet<>(m.headMap(toElement, inclusive));    }    //返回此集合的部分的视图,该部分的元素大于(或等于,如果inclusive为true)fromElement。    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {        return new TreeSet<>(m.tailMap(fromElement, inclusive));    }    //返回此集合的部分的视图,其元素的范围从fromElement(包括)到toElement,排除。    public SortedSet<E> subSet(E fromElement, E toElement) {        return subSet(fromElement, true, toElement, false);    }    //返回该集合的部分的视图,该部分的元素严格小于toElement。    public SortedSet<E> headSet(E toElement) {        return headSet(toElement, false);    }    //返回该集合的元素大于或等于fromElement的部分的视图。    public SortedSet<E> tailSet(E fromElement) {        return tailSet(fromElement, true);    }//返回用于对该集合中的元素进行排序的比较器,如果此集合使用其元素的自然排序,则返回null。    public Comparator<? super E> comparator() {        return m.comparator();    }    //返回此集合中当前的第一个(最低)元素。    public E first() {        return m.firstKey();    }    //返回此集合中当前的最后(最高)元素。    public E last() {        return m.lastKey();    }    //返回该集合中最大的元素严格小于给定的元素,如果没有这样的元素,则返回null。    public E lower(E e) {        return m.lowerKey(e);    }    //返回该集合中最大的元素小于或等于给定的元素,如果没有这样的元素,返回null。    public E floor(E e) {        return m.floorKey(e);    }    //返回此集合中最小元素大于或等于给定元素,如果没有此元素,则返回null。    public E ceiling(E e) {        return m.ceilingKey(e);    }    //返回严格大于给定元素的此集合中的最小元素,如果没有此元素则返回null。    public E higher(E e) {        return m.higherKey(e);    }    //检索并删除第一个(最低)元素,如果此集合为空,则返回null。    public E pollFirst() {        Map.Entry<E,?> e = m.pollFirstEntry();        return (e == null) ? null : e.getKey();    }    //检索并删除最后一个(最高)元素,如果此集合为空,则返回null。    public E pollLast() {        Map.Entry<E,?> e = m.pollLastEntry();        return (e == null) ? null : e.getKey();    }    //返回此TreeSet实例的浅层副本。    public Object clone() {        TreeSet<E> clone;        try {            clone = (TreeSet<E>) super.clone();        } catch (CloneNotSupportedException e) {            throw new InternalError(e);        }        clone.m = new TreeMap<>(m);        return clone;    }//在此集合中的元素上创建一个后期绑定和故障切换的Spliterator。    public Spliterator<E> spliterator() {        return TreeMap.keySpliteratorFor(m);    }}




原创粉丝点击