从源码分析SortedSet与NavigableSet

x33g5p2x  于2021-12-06 转载在 其他  
字(2.2k)|赞(0)|评价(0)|浏览(196)

SortedSet

Set集合本身不具有排序的功能,SortedSet接口就提供了排序的操作。

SortedSet直接继承至Set接口。

public interface SortedSet<E> extends Set<E>

基本方法

SortedSet接口中声明了一些操作Set集合的基本方法。

public interface SortedSet<E> extends Set<E> {
   //返回用于对该集合中的元素进行排序的比较器
   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,它对SortedSet进行了扩展,具有了为给定搜索目标报告最接近匹配项的导航方法。

public interface NavigableSet<E> extends SortedSet<E>

基本方法

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 。 如果fromElement和toElement相等,则返回的集合为空,除非fromInclusive和toInclusive都为真。
    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                           E toElement,   boolean toInclusive);

    //返回该集合的部分的视图,其元素小于(或等于,如果inclusive为真) toElement 。
    NavigableSet<E> headSet(E toElement, boolean inclusive);

    //返回此集合的部分的视图,其元素大于(或等于,如果inclusive为真) 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);
}

相关文章

微信公众号

最新文章

更多