C

Core Java tutorial for beginners

Clean • Professional

NavigableSet in Java – Methods, TreeSet Implementation & Examples

3 minute

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

learn code with durgesh images

Key Navigation Methods

MethodDescription
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

MethodDescription
first()Returns the smallest element
last()Returns the largest element
pollFirst()Removes + returns first element
pollLast()Removes + returns last element

Reverse Order (Descending View)

MethodDescription
descendingSet()Returns Set in reverse order
descendingIterator()Iterates in reverse

Range & Subset Methods

MethodDescription
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

ClassDescription
TreeSetMain implementation using a Red-Black Tree
ConcurrentSkipListSetThread-safe implementation

Time Complexity (TreeSet)

OperationTime
add()O(log n)
remove()O(log n)
search()O(log n)
traversalO(n)

Difference Between NavigableSet vs SortedSet

FeatureSortedSetNavigableSet
DefinitionProvides a set of elements in sorted (ascending) orderExtends SortedSet with advanced navigation methods
OrderSorted order onlySorted order + reverse order support
Navigation MethodsNot availablelower(), floor(), ceiling(), higher()
Element RetrievalOnly first() and last()pollFirst(), pollLast() (remove + return)
Range ViewssubSet(), headSet(), tailSet()Enhanced versions with inclusive/exclusive control
Reverse ViewNot availabledescendingSet()
Interface vs. ExtensionBase interfaceSub-interface of SortedSet
Common ImplementationTreeSet (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).

Article 0 of 0