Clean • Professional
A NavigableSet in Java is a sorted set that allows you to navigate elements using closest-match search methods. It is part of the java.util package and is most commonly implemented by TreeSet.
It extends SortedSet and provides powerful navigation features such as:
lower()floor()ceiling()higher()descendingSet()subSet(), headSet(), tailSet()All elements in a NavigableSet are unique and stored in sorted order (natural order or a custom Comparator).
Set
└── SortedSet
└── NavigableSet
└── TreeSet (Main Implementation)
Other implementation: ConcurrentSkipListSet (thread-safe)
A NavigableSet is a Set that:
It is perfect when you need sorted unique elements with smart lookup methods.

| Method | Description |
|---|---|
lower(e) | Returns element strictly less than e |
floor(e) | Returns element less than or equal to e |
ceiling(e) | Returns element greater than or equal to e |
higher(e) | Returns element strictly greater than e |
| Method | Description |
|---|---|
first() | Returns the smallest element |
last() | Returns the largest element |
pollFirst() | Removes + returns first element |
pollLast() | Removes + returns last element |
| Method | Description |
|---|---|
descendingSet() | Returns Set in reverse order |
descendingIterator() | Iterates in reverse |
| Method | Description |
|---|---|
subSet(from, to) | Elements between from and to |
subSet(from, boolean, to, boolean) | Range with inclusive/exclusive control |
headSet(to) | Elements less than to |
tailSet(from) | Elements greater than/equal to from |
import java.util.*;
public class NavigableSetExample {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(10);
set.add(20);
set.add(30);
set.add(40);
System.out.println("Set: " + set);
System.out.println("lower(20): " + set.lower(20)); // 10
System.out.println("floor(20): " + set.floor(20)); // 20
System.out.println("ceiling(25): " + set.ceiling(25)); // 30
System.out.println("higher(30): " + set.higher(30)); // 40
System.out.println("Descending Set: " + set.descendingSet());
}
}
Output
Set: [10, 20, 30, 40]
lower(20): 10
floor(20): 20
ceiling(25): 30
higher(30): 40
Descending Set: [40, 30, 20, 10]
Use a NavigableSet when you need:
10–50)Common real-world use cases:
| Class | Description |
|---|---|
| TreeSet | Main implementation using a Red-Black Tree |
| ConcurrentSkipListSet | Thread-safe implementation |
| Operation | Time |
|---|---|
| add() | O(log n) |
| remove() | O(log n) |
| search() | O(log n) |
| traversal | O(n) |
| Feature | SortedSet | NavigableSet |
|---|---|---|
| Definition | Provides a set of elements in sorted (ascending) order | Extends SortedSet with advanced navigation methods |
| Order | Sorted order only | Sorted order + reverse order support |
| Navigation Methods | Not available | lower(), floor(), ceiling(), higher() |
| Element Retrieval | Only first() and last() | pollFirst(), pollLast() (remove + return) |
| Range Views | subSet(), headSet(), tailSet() | Enhanced versions with inclusive/exclusive control |
| Reverse View | Not available | descendingSet() |
| Interface vs. Extension | Base interface | Sub-interface of SortedSet |
| Common Implementation | TreeSet (also implements SortedSet) | TreeSet, ConcurrentSkipListSet |
lower(), floor(), ceiling(), higher().subSet(), headSet(), tailSet().descendingSet().pollFirst(), pollLast().