Java NavigableSet

NavigableSet represents a navigable set in Java Collection Framework. The NavigableSet interface inherits from the SortedSet interface. It behaves like a SortedSet with the exception that we have navigation methods available in addition to the sorting mechanisms of the SortedSet. NavigableSet interface can navigate the set in reverse order compared to the order defined in SortedSet. A NavigableSet may be accessed and traversed in either ascending or descending order. The classes that implement this interface are, TreeSet and ConcurrentSkipListSet.

How to use NavigableSet

In Java, we must import the java.util.NavigableSet package to use NavigableSet. Once we import the package, here's how we can create navigable sets.


                        // NavigableSet implementation by TreeSet class
                        NavigableSet<String> numbers = new TreeSet<>();
                    
Methods of NavigableSet

The NavigableSet provides various methods that can be used to navigate over its elements.

Method Name Description
descendingSet() reverses the order of elements in a set.
descendingIterator() returns an iterator that can be used to iterate over a set in reverse order.
ceiling() returns the lowest element among those elements that are greater than or equal to the specified element.
floor() returns the greatest element among those elements that are less than or equal to the specified element.
higher() returns the lowest element among those elements that are greater than the specified element.
lower() returns the greatest element among those elements that are less than the specified element.
pollFirst() returns and removes the first element from the set.
pollLast() returns and removes the last element from the set.
Implementation of NavigableSet in TreeSet Class

                        import java.util.NavigableSet;
                        import java.util.TreeSet;
                        
                        public class NavigableSetExample {
                        
                            public static void main(String[] args) {
                                // Creating NavigableSet using the TreeSet
                                NavigableSet<Integer> numbers = new TreeSet<>();
                        
                                // Insert elements to the set
                                numbers.add(1);
                                numbers.add(2);
                                numbers.add(3);
                                System.out.println("NavigableSet: " + numbers);
                        
                                // Access the first element
                                int firstElement = numbers.first();
                                System.out.println("First Number: " + firstElement);
                        
                                // Access the last element
                                int lastElement = numbers.last();
                                System.out.println("Last Element: " + lastElement);
                        
                                // Remove the first element
                                int number1 = numbers.pollFirst();
                                System.out.println("Removed First Element: " + number1);
                        
                                // Remove the last element
                                int number2 = numbers.pollLast();
                                System.out.println("Removed Last Element: " + number2);
                        
                            }
                        }