NavigableSet interface - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, November 26, 2016

NavigableSet interface

The NavigableSet interface extends SortedSet interface and declare the behavior of a collection that supports the retrieval of elements based on the closest match to a given value or values.

NavigableSet is a generic interface that has this declaration:

public interface NavigableSet<E> extends SortedSet<E>

Here, E specifies the type of objects that the list will hold.

In addition to the methods defined by SortedSet, NavigableSet defines some of its own method.

Method Description
E ceiling(E obj) Searches the set for the smallest element e such that e >= obj. If such an element is found, it is returned. Otherwise, null is returned.
Iterator<E> descendingIterator( ) Returns an iterator that moves from the greatest to least. In other words, it returns a reverse iterator.
NavigableSet<E> descendingSet( ) Returns a NavigableSet that is the reverse of the invoking set. The resulting set is backed by the invoking set.
E floor(E obj) Searches the set for the largest element e such that e <= obj . If such an element is found, it is returned. Otherwise, null is returned.
NavigableSet<E> headSet(E upperBound, boolean incl) Returns a NavigableSet that includes all elements from the invoking set that are less than upperBound. If incl is true, then an element equal to upperBound is included. The resulting set is backed by the invoking set.
E higher(E obj) Searches the set for the largest element e such that e > obj. If such an element is found, it is returned. Otherwise, null is returned.
E lower(E obj) Searches the set for the largest element e such that e < obj. If such an element is found, it is returned. Otherwise, null is returned.
E pollFirst( ) Returns the first element, removing the element in the process. Because the set is sorted, this is the element with the least value. null is returned if the set is empty.
E pollLast( ) Returns the last element, removing the element in the process. Because the set is sorted, this is the element with the greatest value. null is returned if the set is empty.
NavigableSet<E> subSet(E lowerBound, boolean lowIncl, E upperBound, boolean highIncl) Returns a NavigableSet that includes all elements from the invoking set that are greater than lowerBound and less than upperBound. If lowIncl is true, then an element equal to lowerBound is included. If highIncl is true, then an element equal to upperBound is included. The resulting set is backed by the invoking set.
NavigableSet<E> tailSet(E lowerBound, boolean incl) Returns a NavigableSet that includes all elements from the invoking set that are greater than lowerBound. If incl is true, then an element equal to lowerBound is included. The resulting set is backed by the invoking set.

Most Commonly thrown Exceptions in NavigableSet

Exception Description
ClassCastException occurs when an attempt is made to add an incompatible object in a set.
NullPointerException will throw when you try to store a null object and null element is not allowed in the set.
IllegalArgumentException will throw when an invalid argument is used.

No comments :

Post a Comment