今天看项目代码,项目中用到了TreeSet类。这个类虽然用的不多,但是起码知道该类是SortedSet接口的子类(其实是子子类),内部实现是红黑树。继续看代码又发现了下面一行代码:
TreeSet<Long> notifyTreeSet = ....额,此处省略函数,....;
...
NavigableSet<Long> unreadNotifyIdSet = notifyTreeSet.tailSet(cursor, false);
瞬间脑袋晕乎乎的,NavigableSet又是个什么鬼,tailSet函数又是个啥玩意。
翻了翻源码,是这样的
// TreeSet类
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable {...}
// NavigableSet 接口
public interface NavigableSet<E> extends SortedSet<E> {...}
TreeSet继承了抽象类AbstractSet和NavigableSet接口,而NavigableSet接口又继承了SortedSet接口,这下就有了大致的了解,再到网上盗了一张图片,就能更直观的了解了。
因此NavigableSet拥有的性质,TreeSet都拥有,下面看看NavigableSet拥有哪些性质,再看源码(简写):
public interface NavigableSet<E> extends SortedSet<E> {
/**
* Returns the greatest element in this set strictly less than the
* given element, or {@code null} if there is no such element.
* 严格返回小于元素e的最大元素,没有则返回null
*/
E lower(E e);
/**
* Returns the greatest element in this set less than or equal to
* the given element, or {@code null} if there is no such element.
*返回小于或者等于元素e的最大元素,没有则返回null
*/
E floor(E e);
/**
* Returns the least element in this set greater than or equal to
* the given element, or {@code null} if there is no such element.
* 返回大于或者等于元素e的最小元素,没有则返回null
*/
E ceiling(E e);
/**
* Returns the least element in this set strictly greater than the
* given element, or {@code null} if there is no such element.
* 严格返回大于元素e的最小元素,没有则返回null
*/
E higher(E e);
/**
* Retrieves and removes the first (lowest) element,
* or returns {@code null} if this set is empty.
* 返回并移除集合中第一个元素,没有则返回null
*/
E pollFirst();
/**
* Retrieves and removes the last (highest) element,
* or returns {@code null} if this set is empty.
* 返回并移除集合中最后一个元素,没有则返回null
*/
E pollLast();
/**
* Returns an iterator over the elements in this set, in ascending order.
* 按升序返回此集合中元素的迭代器
*/
Iterator<E> iterator();
/**
* Returns a reverse order view of the elements contained in this set.
* 返回包含该集合中元素的逆序视图
*/
NavigableSet<E> descendingSet();
/**
* Returns an iterator over the elements in this set, in descending order.
* 按降序返回此集合中元素的迭代器。
*/
Iterator<E> descendingIterator();
/**
* Returns a view of the portion of this set whose elements range from
* {@code fromElement} to {@code toElement}. If {@code fromElement} and
* {@code toElement} are equal, the returned set is empty unless {@code
* fromInclusive} and {@code toInclusive} are both true.
* 返回集合中在fromElement到toElement的元素构成的新集合,fromInclusive和toInclusive表示是否包含头和尾
*/
NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
E toElement, boolean toInclusive);
/**
* Returns a view of the portion of this set whose elements are less than
* (or equal to, if {@code inclusive} is true) {@code toElement}. The
* returned set is backed by this set, so changes in the returned set are
* reflected in this set, and vice-versa. The returned set supports all
* optional set operations that this set supports.
* 返回小于toElement的元素构成的新集合,inclusive表示是否包含元素toElement
*/
NavigableSet<E> headSet(E toElement, boolean inclusive);
/**
* Returns a view of the portion of this set whose elements are greater
* than (or equal to, if {@code inclusive} is true) {@code fromElement}.
* The returned set is backed by this set, so changes in the returned set
* are reflected in this set, and vice-versa. The returned set supports
* all optional set operations that this set supports.
* 返回大于toElement的元素构成的新集合,inclusive表示是否包含元素fromElement
*/
NavigableSet<E> tailSet(E fromElement, boolean inclusive);
/**
* <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.
* 返回集合中在fromElement到toElement的元素构成的新集合,fromInclusive和toInclusive表示包含头不包含尾
*/
SortedSet<E> subSet(E fromElement, E toElement);
/**
* <p>Equivalent to {@code headSet(toElement, false)}.
* 返回小于toElement的元素构成的新集合,不包含元素toElement
*/
SortedSet<E> headSet(E toElement);
/**
* <p>Equivalent to {@code tailSet(fromElement, true)}.
* 返回大于toElement的元素构成的新集合,不包含元素toElement
*/
SortedSet<E> tailSet(E fromElement);
}