Java集合源码学习(12)_SortedSet接口和NavigableSet接口

时间:2022-01-08 14:54:33

一:SortedSet接口

有序的Set,排序的标准为:设置的Comparator或者自然排序(compareTo()方法); 所有的元素必须实现Comparable接口或者Set设置了Comparator接口;
Method Summary
 Comparator<? super E> comparator() 
          Returns the comparator used to order the elements in this set, or null if this set uses the natural orderingof its elements.
 E first() 
          Returns the first (lowest) element currently in this set.
 SortedSet<E> headSet(E toElement) 
          Returns a view of the portion of this set whose elements are strictly less than toElement.
 E last() 
          Returns the last (highest) element currently in this set.
 SortedSet<E> subSet(E fromElement, E toElement) 
          Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
 SortedSet<E> tailSet(E fromElement) 
          Returns a view of the portion of this set whose elements are greater than or equal to fromElement.

二:NavigableSet接口

继承了SortedSet接口 增加了若干接口来获得离某个元素最近的一些元素;
Method Summary
 E ceiling(E e) 
          Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
 Iterator<E> descendingIterator() 
          Returns an iterator over the elements in this set, in descending order.
 NavigableSet<E> descendingSet() 
          Returns a reverse order view of the elements contained in this set.
 E floor(E e) 
          Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
 SortedSet<E> headSet(E toElement) 
          Returns a view of the portion of this set whose elements are strictly less than toElement.
 NavigableSet<E> headSet(E toElement,
boolean inclusive)
 
          Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true)toElement.
 E higher(E e) 
          Returns the least element in this set strictly greater than the given element, or null if there is no such element.
 Iterator<E> iterator() 
          Returns an iterator over the elements in this set, in ascending order.
 E lower(E e) 
          Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
 E pollFirst() 
          Retrieves and removes the first (lowest) element, or returns null if this set is empty.
 E pollLast() 
          Retrieves and removes the last (highest) element, or returns null if this set is empty.
 NavigableSet<E> subSet(E fromElement,
boolean fromInclusive, E toElement, boolean toInclusive)
 
          Returns a view of the portion of this set whose elements range from fromElement to toElement.
 SortedSet<E> subSet(E fromElement, E toElement) 
          Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
 SortedSet<E> tailSet(E fromElement) 
          Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
 NavigableSet<E> tailSet(E fromElement,
boolean inclusive)
 
          Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

 三:TreeMap

内部是通过NavigableMap的形式来存储的;
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable {

private transient NavigableMap<E, Object> m;