The NavigableSet interface is present in the java.util package and extends the SortedSet interface. It represents a sorted collection that provides navigation methods to retrieve elements closest to a given element.
- Maintains unique elements in sorted order (ascending by default).
- Provides navigation methods like lower(), floor(), ceiling(), and higher() to access nearby elements.
- Implemented mainly by the TreeSet class in the Java Collection Framework.
Declaration Syntax
Since NavigableSet is an interface, objects cannot be created directly. It is typically implemented using the TreeSet class.
public interface NavigableSet<E> extends SortedSet<E>
import java.util.NavigableSet;
import java.util.TreeSet;
public class GFG {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(50);
set.add(20);
set.add(40);
set.add(10);
System.out.println(set);
System.out.println("Lower than 40: " + set.lower(40));
System.out.println("Higher than 40: " + set.higher(40));
}
}
Output
Navigable Set: [10, 20, 40, 50, 70]
Explanation
- TreeSet implements the NavigableSet interface.
- Elements are automatically sorted in ascending order.
- lower() and higher() return the closest elements before and after a given value.
Hierarchy of NavigableSet
The NavigableSet interface extends the SortedSet interface in the Java Collection Framework.

TreeSet is a class that implements NavigableSet, which in turn extends SortedSet, which extends Set.
Declaration of NavigableSet
NavigableSet<Type> setName;
Note: "Type" is the type of element in the set (e.g. integer, String, etc) and setName is the name of the variable.
Creating NavigableSet Objects
We can not create a NavigableSet directly since it's an interface. Instead we use a class like TreeSet that implements it. With the help of generics, we can define the type of objects the set will store. This type-safe set can be defined as:
NavigableSet<Obj> set = new TreeSet<Obj>();
Example:
import java.util.NavigableSet;
import java.util.TreeSet;
public class Geeks
{
public static void main(String[] args)
{
NavigableSet<Integer> ns = new TreeSet<>();
ns.add(0);
ns.add(1);
ns.add(2);
ns.add(3);
ns.add(4);
ns.add(5);
ns.add(6);
// Get a reverse view of the navigable set
NavigableSet<Integer> revNs = ns.descendingSet();
// Print the normal and reverse views
System.out.println("Normal order: " + ns);
System.out.println("Reverse order: " + revNs);
NavigableSet<Integer> t = ns.tailSet(3, true);
System.out.println("3 or more: " + t);
System.out.println("lower(3): " + ns.lower(3));
System.out.println("floor(3): " + ns.floor(3));
System.out.println("higher(3): " + ns.higher(3));
System.out.println("ceiling(3): " + ns.ceiling(3));
System.out.println("pollFirst(): " + ns.pollFirst());
System.out.println("Navigable Set: " + ns);
System.out.println("pollLast(): " + ns.pollLast());
System.out.println("Navigable Set: " + ns);
System.out.println("pollFirst(): " + ns.pollFirst());
System.out.println("Navigable Set: " + ns);
System.out.println("pollFirst(): " + ns.pollFirst());
System.out.println("Navigable Set: " + ns);
System.out.println("pollFirst(): " + ns.pollFirst());
System.out.println("Navigable Set: " + ns);
System.out.println("pollFirst(): " + ns.pollFirst());
System.out.println("pollLast(): " + ns.pollLast());
}
}
Output:

Performing Various Operations on NavigableSet
1. Adding Elements
Elements can be added using the add() method. In a TreeSet, elements are automatically sorted and duplicate values are ignored.
import java.util.*;
class Geeks {
public static void main(String[] args)
{
NavigableSet<String> set = new TreeSet<>();
set.add("A");
set.add("B");
set.add("C");
set.add("A");
System.out.println(set);
}
}
Output
NavigableSet: [A, B, C]
2. Accessing Elements
After adding the elements, if we wish to access the elements, we can use inbuilt methods like contains(), first(), last(), etc.
import java.util.*;
import java.io.*;
class Geeks {
public static void main(String[] args)
{
NavigableSet<String> ts = new TreeSet<String>();
// Elements are added using add() method
ts.add("A");
ts.add("B");
ts.add("C");
ts.add("A");
System.out.println("NavigableSet: " + ts);
String s = "D";
// Check if the above string exists in
// the NavigableSet or not
System.out.println("D exists in the NavigableSet?: "
+ ts.contains(s));
// Print the first element in
// the NavigableSet
System.out.println("First Element of NavigableSet: "
+ ts.first());
// Print the last element in
// the NavigableSet
System.out.println("Last Element of NavigableSet: "
+ ts.last());
}
}
Output
NavigableSet: [A, B, C] D exists in the NavigableSet?: false First Element of NavigableSet: A Last Element of NavigableSet: C
3. Removing Elements
The values can be removed from the NavigableSet using the remove(), pollFirst(), pollLast().
import java.io.*;
import java.util.*;
class Geeks {
public static void main(String[] args)
{
NavigableSet<String> ts = new TreeSet<String>();
// Elements are added using add() method
ts.add("A");
ts.add("B");
ts.add("C");
ts.add("B");
ts.add("D");
ts.add("E");
System.out.println("NavigableSet: " + ts);
// Removing the element b
ts.remove("B");
System.out.println("After removing element " + ts);
// Remove the First element of TreeSet
ts.pollFirst();
System.out.println(
"After the removal of First Element " + ts);
// Remove the Last element of TreeSet
ts.pollLast();
System.out.println(
"After the removal of Last Element " + ts);
}
}
Output
NavigableSet: [A, B, C, D, E] After removing element [A, C, D, E] After the removal of First Element [C, D, E] After the removal of Last Element [C, D]
4. Iterating Elements
There are various ways to iterate through the NavigableSet. The most famous one is to use the enhanced for loop.
import java.util.*;
import java.io.*;
class Geeks {
public static void main(String[] args)
{
NavigableSet<String> ts = new TreeSet<String>();
// Elements are added using add() method
ts.add("C");
ts.add("D");
ts.add("E");
ts.add("A");
ts.add("B");
ts.add("Z");
// Iterating though the NavigableSet
for (String i : ts)
System.out.print(i + ", ");
}
}
Output
A, B, C, D, E, Z,
Methods of NavigableSet Interface
The following are the methods present in the NavigableSet interface.Â
Methods | Description |
|---|---|
| Â 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. |
| descendingIterator() | Returns an iterator over the elements in this set, in descending order. |
| descendingSet() | Returns a reverse order view of the elements contained in this set. |
| 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. |
| headSet(E toElement) | Returns a view of the portion of this set whose elements are strictly less than toElement. |
| 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. |
| 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() | Returns an iterator over the elements in this set, in ascending order. |
| lower(E e) | Returns the greatest element in this set strictly less than the given element, or null if there is no such element. |
| pollFirst() | Retrieves and removes the first (lowest) element, or returns null if this set is empty. |
| pollLast() | Retrieves and removes the last (highest) element, or returns null if this set is empty. |
| Returns a view of the portion of this set whose elements range from fromElement to toElement. | |
| subSet(E fromElement, E toElement) | Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. |
| tailSet(E fromElement) | Returns a view of the portion of this set whose elements are greater than or equal to fromElement. |
| 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. |
                                                                       Â
Methods Inherited from Interface java.util.SortedSet
Method | Description |
|---|---|
| comparator()Â | This method returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements. |
| first() | This method returns the first(lowest) element present in this set. |
| last() | This method returns the last(highest) element present in the set. |
| spliterator() | Creates a Spliterator over the elements in this sorted set. |
Methods Inherited from Interface java.util.Set
Method | Description |
|---|---|
| add(element) | This method is used to add a specific element to the set. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. |
| addAll(collection)Â | This method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. |
| clear()Â Â | This method is used to remove all the elements from the set but not delete the set. The reference for the set still exists. |
| contains(element) | This method is used to check whether a specific element is present in the Set or not. |
| containsAll(collection)Â | This method is used to check whether the set contains all the elements present in the given collection or not. This method returns true if the set contains all the elements and returns false if any of the elements are missing. |
| equals() | Compares the specified object with this set for equality. |
| hashCode()Â | This method is used to get the hashCode value for this instance of the Set. It returns an integer value which is the hashCode value for this instance of the Set. |
| isEmpty() | This method is used to check if a NavigableSet is empty or not. |
| remove(element) | This method is used to remove the given element from the set. This method returns True if the specified element is present in the Set otherwise it returns False. |
| removeAll(collection) | This method is used to remove all the elements from the collection which are present in the set. This method returns true if this set changed as a result of the call. |
| retainAll(collection) | This method is used to retain all the elements from the set which are mentioned in the given collection. This method returns true if this set changed as a result of the call. |
| size() | This method is used to get the size of the set. This returns an integer value which signifies the number of elements. |
| toArray() | This method is used to form an array of the same elements as that of the Set. |
| Â toArray(T[] a) | Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. |
Methods Declared in Interface java.util.Collection
| Method | Description |
|---|---|
| parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
| removeIf(Predicate<? super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
| stream() | Returns a sequential Stream with this collection as its source. |
| toArray?(IntFunction<T[]> generator) | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
Methods Declared in Interface java.lang.Iterable
| Method | Description |
|---|---|
| forEach(Consumer<? super T> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |