In Java, the NavigableSet is a subtype of the SortedSet interface. It allows us to perform various operations like getting the closest matches for a given element, descending order iteration, and others. It provides methods to navigate through the elements in the set.
For Example, The NavigableSet interface allows us to navigate through the set in both ascending and descending order, unlike the SortedSet which only supports the ascending order. The classes that implement the NavigableSet interface are TreeSet and ConcurrentSkipListSet
- NavigableSet extends SortedSet and thus provides methods like first(), last(), headSet(), tailSet(), etc.
- It allows you to navigate in both directions ascending and descending order
- The most common implementation of NavigableSet is TreeSet.
Example: This example demonstrates creating a NavigableSet using TreeSet and adding elements to it, which automatically sorts them in ascending order.
Java
// Java program to demonstrates
// the working of NavigableSet
import java.util.*;
public class Geeks {
public static void main(String[] args) {
NavigableSet<Integer> ns = new TreeSet<>();
// Add elements to the set
ns.add(10);
ns.add(20);
ns.add(30);
ns.add(40);
ns.add(50);
System.out.println("Navigable Set: " + ns);
}
}
OutputNavigable Set: [10, 20, 30, 40, 50]
The diagram below demonstrates the inheritance structure in Java’s collection framework related to sets.

TreeSet is a class that implements NavigableSet, which in turn extends SortedSet, which extends Set.
Declaration of NavigableSet
In Java, the declaration of NavigableSet can be declared as:
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: This example demonstrates how to use various methods of NavigableSet like descedingSet(), tailSet(), lower(), pollFirst() and pollLast() to manipulate and navigate a sorted set in both normal and reverse order.
Java
// Java Program to demostrates the
// working of various methods of NavigableSet
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: We can use the add() method to insert elements to the NavigableSet. Elements are stored in the sorted order, no duplicates are allowed and null values are also not accepted by the NavigableSet.
Example: This example demonstrates adding elements to a NavigableSet using add(), where duplicates are ignored and the elements are sorted in ascending order.
Java
// Java Program to demonstrates the working of add()
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);
}
}
OutputNavigableSet: [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.
Example: This example demonstrates adding elements to a NavigableSet, checking for an element’s existence, and retrieving the first and last elements.
Java
// Java program to demonstrates the
// working of contains(), first() and last() method
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());
}
}
OutputNavigableSet: [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().
Example: This example demonstrates removing elements from the NavigableSet.
Java
// Java Program to demonstrates the working of remove(),
// pollFirst() and pollLast() method
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);
}
}
OutputNavigableSet: [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.
Example: This example demonstrates adding elements to a NavigableSet and iterating through it in ascending order.
Java
// Java Program to iterate through NavigableSet
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 + ", ");
}
}
Methods
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. |
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. |
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. |
Similar Reads
NavigableSet in Java
In Java, the NavigableSet is a subtype of the SortedSet interface. It allows us to perform various operations like getting the closest matches for a given element, descending order iteration, and others. It provides methods to navigate through the elements in the set. For Example, The NavigableSet i
10 min read
NavigableSet ceiling() method in Java
The ceiling() method of NavigableSet interface in Java is used to return the least element in this set greater than or equal to the given element, or null if there is no such element. Syntax: E ceiling(E ele) Where, E is the type of elements maintained by this Set container. Parameters: This functio
2 min read
NavigableSet descendingIterator() method in Java
The descendingIterator() method of NavigableSet interface in Java is used to return an iterator over the elements in this set, in descending order. This iterator can be then used to iterate over the elements of the set. The iterator returned by this set is also equivalent to descendingSet().iterator
2 min read
NavigableSet descendingSet() method in Java
The descendingSet() method of NavigableSet interface in Java is used to return a reverse order view of the elements contained in this set. The descending set is backed by this set, so any changes to the set are reflected in the descending set, and vice-versa. If any of the set is modified while an i
2 min read
NavigableSet floor() method in Java
The floor() method of NavigableSet interface in Java is used to return the greatest element in this set less than or equal to the given element, or null if there is no such element exists in the set. Syntax: E floor(E ele) Where, E is the type of elements maintained by this Set container. Parameters
2 min read
NavigableSet headSet() method in Java
The headSet() method of NavigableSet interface in Java is used to return a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.The
2 min read
NavigableSet higher() method in Java
The higher() method of NavigableSet interface in Java is used to return the least element in this set strictly greater than the given element, or null if there is no such element exists. Syntax: E higher(E ele) Where, E is the type of elements maintained by this Set container. Parameters: This funct
2 min read
NavigableSet iterator() method in Java
The iterator() method of NavigableSet interface in Java is used to return an iterator over the elements in this set, in ascending order. This iterator can be then used to iterate over the elements of the set. Syntax: Iterator<E> iterator() Where, E is the type of elements maintained by this Se
2 min read
NavigableSet lower() method in Java
The lower() method of NavigableSet interface in Java is used to return the greatest element in this set strictly less than the given element, or null if there is no such element exists in the set. Syntax: E lower(E ele) Where, E is the type of elements maintained by this Set container. Parameters: T
2 min read
NavigableSet pollFirst() method in Java
The pollFirst() method of NavigableSet interface in Java is used to retrieves and removes the first (lowest) element, or returns null if this set is empty. Syntax: E pollFirst() Where, E is the type of elements maintained by this Set container. Parameters: This function does not accepts any paramete
1 min read