Sorted & Navigable

SortedSet/SortedMap

Map the key is a Set, so almost all of Java using the corresponding Map Set are implemented.

TreeSet -> NavigableSet -> SortedSet

So TreeSet is an implementation of SortedSet. (The other is ConcurrentSkipListSet)

public interface SortedSet<E> extends Set<E> {
    // 返回使用的comparator,如果使用自然排序(compareTo),则返回null
    Comparator<? super E> comparator();
    
	// 返回该set中元素范围从fromElement(包括)到toElement(不包括)的部分的视图。
    // 任何对返回的set的操作都会改变原来的set,反之亦然
    // 如果fromElement和toElement相等,则返回一个空的set
    SortedSet<E> subSet(E fromElement, E toElement);

    // 返回小于toElement的部分的视图
    SortedSet<E> headSet(E toElement);

    // 返回大于或等于fromElement的那部分的视图。
    SortedSet<E> tailSet(E fromElement);

    // 返回第一个(最小的)元素。
    E first();

    // 返回最后一个(最大的)元素。
    E last();

    default Spliterator<E> spliterator(){}
}

Note: Internal SortedSet only compareTo / comparator compares sort, so for incoming element, also using compareTo / comparator to find the range, which can not itself does not set a pass inside the element, you can only use compareTo / comparator that is can.

public interface NavigableSet<E> extends SortedSet<E> {
    // 返回小于e的最大元素,如果没有,则返回null。
	E lower(E e);
    
    // 返回小于或等于e的最大元素,如果没有,则返回null。
    E floor(E e);
    
    // 返回大于或等于e的最小元素,如果没有,则返回null。
    E ceiling(E e);
    
    // 返回大于e的最小元素,如果没有,则返回null。
    E higher(E e);
    
    // 检索并删除第一个(最小的)元素,如果set为空,则返回null。
    E pollFirst();
    
    // 检索并删除最后一个(最大的)元素,如果set为空,则返回null。
    E pollLast();
    
    Iterator<E> iterator();
    
    // 返回降序的set的视图
    NavigableSet<E> descendingSet();
    Iterator<E> descendingIterator();
    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                           E toElement,   boolean toInclusive);
    NavigableSet<E> headSet(E toElement, boolean inclusive);

    NavigableSet<E> tailSet(E fromElement, boolean inclusive);
    
    SortedSet<E> subSet(E fromElement, E toElement);
    SortedSet<E> headSet(E toElement);
    SortedSet<E> tailSet(E fromElement);
}

Note: The internal NavigableSet is only compareTo / comparator compares sort.

You can see, NavigableSet inherited SortedSet, increasing the range to find a single element, descending set, and enhanced partial set of methods to obtain.

Guess you like

Origin www.cnblogs.com/demojie/p/12596144.html