Java NavigableSet Interface

In Java, the NavigableSet interface is a subinterface of the SortedSet interface that provides methods for navigating the set in both forward and reverse directions. A NavigableSet extends the functionality of a SortedSet by providing additional methods for finding the closest matches to a given element, and for iterating over the elements in the set in both directions.

The NavigableSet interface provides the following additional methods:

  • ceiling(e): Returns the least element in the set greater than or equal to e, or null if there is no such element.
  • floor(e): Returns the greatest element in the set less than or equal to e, or null if there is no such element.
  • higher(e): Returns the least element in the set strictly greater than e, or null if there is no such element.
  • lower(e): Returns the greatest element in the set strictly less than e, or null if there is no such element.
  • pollFirst(): Retrieves and removes the first (lowest) element of the set, or returns null if the set is empty.
  • pollLast(): Retrieves and removes the last (highest) element of the set, or returns null if the set is empty.
  • descendingSet(): Returns a reverse-order view of the set.

Here is an example of how to use the NavigableSet interface in Java:

import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetExample {
    public static void main(String[] args) {
        // Create a new TreeSet
        NavigableSet<Integer> set = new TreeSet<>();

        // Add elements to the set
        set.add(3);
        set.add(1);
        set.add(2);

        // Print the set
        System.out.println(set);

        // Print the ceiling of 2
        System.out.println("Ceiling of 2: " + set.ceiling(2));

        // Print the floor of 2
        System.out.println("Floor of 2: " + set.floor(2));

        // Print the higher of 2
        System.out.println("Higher of 2: " + set.higher(2));

        // Print the lower of 2
        System.out.println("Lower of 2: " + set.lower(2));

        // Print and remove the first element of the set
        System.out.println("Poll first: " + set.pollFirst());

        // Print and remove the last element of the set
        System.out.println("Poll last: " + set.pollLast());

        // Print the set in reverse order
        System.out.println("Descending set: " + set.descendingSet());
    }
}
‮oS‬urce:www.theitroad.com

Output:

[1, 2, 3]
Ceiling of 2: 2
Floor of 2: 2
Higher of 2: 3
Lower of 2: 1
Poll first: 1
Poll last: 3
Descending set: [2]

In the example above, we have created a TreeSet of integers, which implements the NavigableSet interface. We have added three elements to the set, which are automatically sorted in ascending order. We have then printed the set, which shows the elements in the sorted order. We have also used the ceiling(), floor(), higher(), and lower() methods to find the closest matches to the element 2 in the set. We have then used the pollFirst() and pollLast() methods to remove the