Java NavigableSet Interface
The NavigableSet
interface extends SortedSet with navigation methods that supports the retrieval of elements based on the closest match to a given value or values. NavigableSet
is a generic interface:
interface NavigableSet<E>
Here, E
specifies the type of objects that the set will contain.
Methods
In addition to the methods defined by SortedSet, NavigableSet
defines some of its own:
 E lower(E element)

Returns the greatest element in this set strictly less than the
element
. If there is no such element thennull
will be returned. It throwsNullPointerException
if theelement
isnull
and this set does not permitnull
.elements.
 E floor(E element)

Returns the greatest element in this set strictly less than or equal the
element
. If there is no such element thennull
will be returned. It throwsNullPointerException
if theelement
isnull
and this set does not permitnull
.elements.
 E ceiling(E element)

Returns the least element in this set greater than or equal to the
element
. If there is no such elementnull
will be returned.
 E higher(E element)

Returns the least element in this set greater than the
element
. If there is no such elementnull
will be returned.
 E pollFirst()

Returns and removes the first element.
null
is returned if the set is empty.
 E pollLast()

Returns and removes the last element.
null
is returned if the set is empty.
 Iterator<E> iterator()
 An iterator over the elements in this set will be returned. It will in ascending order.
 NavigableSet<E> descendingSet()

Returns a
NavigableSet
that is the reverse of the invoking set. Any changes in returnedNavigableSet
will be reflected in original set and viceversa.
 Iterator<E> descendingIterator()
 Returns a reverse iterator.
 NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

Returns a
NavigableSet
whose elements range fromfromElement
totoElement
. IffromInclusive
is true, then the element atfromElement
will be included. IftoInclusive
is true, then the element attoElement
will be included.
 NavigableSet<E> headSet(E element, boolean inclusive)

Returns a
NavigableSet
whose elements are less than (or equal to, ifinclusive
is true)element
.
 NavigableSet<E> tailSet(E element, boolean inclusive)

Returns a
NavigableSet
whose elements are greater than (or equal to, ifinclusive
is true)element
.
TreeSet is a commonly used class that implement NavigableSet
interface.