NavigableSet in Java
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).
Hierarchy of NavigableSet
Set
└── SortedSet
└── NavigableSet
└── TreeSet (Main Implementation)
Other implementation: ConcurrentSkipListSet (thread-safe)
What is NavigableSet?
A NavigableSet is a Set that:
- keeps elements sorted
- allows searching for nearest elements
- supports range-based operations
- provides reverse order view
- allows polling (retrieve + remove) first/last elements
It is perfect when you need sorted unique elements with smart lookup methods.
Methods of NavigableSet

Key Navigation 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 |
First & Last Operations
| Method | Description |
|---|---|
first() | Returns the smallest element |
last() | Returns the largest element |
pollFirst() | Removes + returns first element |
pollLast() | Removes + returns last element |
Reverse Order (Descending View)
| Method | Description |
|---|---|
descendingSet() | Returns Set in reverse order |
descendingIterator() | Iterates in reverse |
Range & Subset Methods
| 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 |
Example of NavigableSet
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]
When to Use NavigableSet?
Use a NavigableSet when you need:
- Sorted unique elements
- Fast lookup for closest values
- Range queries (
10–50) - Reverse-order iteration
- Efficient tree-based operations
Common real-world use cases:
- Leaderboards
- Autocomplete suggestions
- Timelines / schedules (next upcoming event)
- Range filtering (e.g., values between X and Y)
Main Implementations
| Class | Description |
|---|---|
| TreeSet | Main implementation using a Red-Black Tree |
| ConcurrentSkipListSet | Thread-safe implementation |
Time Complexity (TreeSet)
| Operation | Time |
|---|---|
| add() | O(log n) |
| remove() | O(log n) |
| search() | O(log n) |
| traversal | O(n) |
Difference Between NavigableSet vs SortedSet
| 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 |
Points to Remember
- Extends SortedSet; stores unique sorted elements.
- Supports navigation methods:
lower(),floor(),ceiling(),higher(). - Provides range/subset views:
subSet(),headSet(),tailSet(). - Can give reverse order view:
descendingSet(). - Supports polling:
pollFirst(),pollLast(). - Main implementations: TreeSet, ConcurrentSkipListSet.
- Duplicates not allowed; null not allowed in TreeSet (for NavigableSet).
