Java Sets

The Set interface, as discussed in the Java Data Structures document, is no more than a Collection in which it is assumed that elements are not duplicated.

Additionally Java defines two other interfaces SortedSet and NavigableSet which extend Set. There are three Set classes of interest to us:

SortedSet Interface

Comparator<? super E> comparator()
  Returns the comparator used to order the elements in this set, 
    or null if this set uses the natural ordering of its elements.
E first()
  Returns the first (lowest) element currently in this set.
E last()
  Returns the last (highest) 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.
SortedSet<E> tailSet(E fromElement)
  Returns a view of the portion of this set whose elements are 
    greater than or equal to fromElement.
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.
Additionally, it is assumed that the function
Iterator<E> iterator()
returns an Iterator object which traverses the list in order from lowest to highest elements.

NavigableSet Interface

The NavigableSet interface is an extension of SortedSet which adds these features:
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.
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.
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.
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.
Iterator descendingIterator()
  Returns an iterator over the elements in this set, in descending order.
NavigableSet descendingSet()
  Returns a reverse order view of the elements contained in this set.
NavigableSet 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.
NavigableSet 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.
NavigableSet 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.


© Robert M. Kline