Java NavigableSet Interface

Category: Java   Tags: Java, Java Collection, Java Set Interface, 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 then null will be returned. It throws NullPointerException if the element is null and this set does not permit null.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 then null will be returned. It throws NullPointerException if the element is null and this set does not permit null.elements.
E ceiling(E element)
Returns the least element in this set greater than or equal to the element. If there is no such element null will be returned.
E higher(E element)
Returns the least element in this set greater than the element. If there is no such element null 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 returned NavigableSet will be reflected in original set and vice-versa.
Iterator<E> descendingIterator()
Returns a reverse iterator.
NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Returns a NavigableSet whose elements range from fromElement to toElement. If fromInclusive is true, then the element at fromElement will be included. If toInclusive is true, then the element at toElement will be included.
NavigableSet<E> headSet(E element, boolean inclusive)
Returns a NavigableSet whose elements are less than (or equal to, if inclusive is true) element.
NavigableSet<E> tailSet(E element, boolean inclusive)
Returns a NavigableSet whose elements are greater than (or equal to, if inclusive is true) element.

TreeSet is a commonly used class that implement NavigableSet interface.