In Java, the NavigableSet
interface is a part of the Java Collections Framework and extends the SortedSet
interface. It provides methods that allow navigating the set in both directions (ascending and descending), offering greater control over the elements. The NavigableSet
is designed to deal with sets of ordered elements, making it a valuable tool for working with sorted collections.
The NavigableSet
interface offers additional methods that go beyond the basic SortedSet
methods, including:
Here are some of the crucial methods provided by the NavigableSet
interface:
lower(E e)
: Returns the greatest element less than the specified element, or null
if there is no such element.floor(E e)
: Returns the greatest element less than or equal to the specified element, or null
if there is no such element.ceiling(E e)
: Returns the least element greater than or equal to the specified element, or null
if there is no such element.higher(E e)
: Returns the least element greater than the specified element, or null
if there is no such element.pollFirst()
: Removes and returns the first (lowest) element, or null
if the set is empty.pollLast()
: Removes and returns the last (highest) element, or null
if the set is empty.descendingIterator()
: Returns an iterator over the elements in the set in descending order.Let’s see how the NavigableSet
interface can be used in Java with a TreeSet
implementation.
import java.util.*;
public class NavigableSetExample {
public static void main(String[] args) {
// Create a NavigableSet using TreeSet
NavigableSet<Integer> navigableSet = new TreeSet<>();
// Adding elements
navigableSet.add(10);
navigableSet.add(20);
navigableSet.add(5);
navigableSet.add(15);
navigableSet.add(30);
System.out.println("Navigable Set: " + navigableSet);
// Using lower() method
System.out.println("Element lower than 15: " + navigableSet.lower(15));
// Using floor() method
System.out.println("Element less than or equal to 15: " + navigableSet.floor(15));
// Using ceiling() method
System.out.println("Element greater than or equal to 15: " + navigableSet.ceiling(15));
// Using higher() method
System.out.println("Element greater than 15: " + navigableSet.higher(15));
// Using pollFirst() method
System.out.println("Removed first element: " + navigableSet.pollFirst());
// Using pollLast() method
System.out.println("Removed last element: " + navigableSet.pollLast());
// Using descendingIterator() method
Iterator<Integer> descendingIterator = navigableSet.descendingIterator();
System.out.print("Descending order: ");
while (descendingIterator.hasNext()) {
System.out.print(descendingIterator.next() + " ");
}
}
}
TreeSet
object, which implements the NavigableSet
interface.add()
method.lower()
, floor()
, ceiling()
, and higher()
to retrieve elements based on their relative positions.pollFirst()
and pollLast()
methods remove the first and last elements, respectively, while returning them.descendingIterator()
method allows us to iterate over the elements in reverse order.
Navigable Set: [5, 10, 15, 20, 30]
Element lower than 15: 10
Element less than or equal to 15: 15
Element greater than or equal to 15: 15
Element greater than 15: 20
Removed first element: 5
Removed last element: 30
Descending order: 20 15 10
subSet()
, headSet()
, and tailSet()
, you can quickly retrieve ranges of data within a specified range.NavigableSet
can be used to maintain a sorted list of players' scores, allowing easy retrieval of the top players or specific scores.