Curriculum
In Java, NavigableSet
is an interface that extends the SortedSet
interface and provides a way to navigate through a set of elements in either ascending or descending order. It provides methods to perform operations like finding the ceiling/floor elements, iterating over the set in reverse order, and performing other navigational operations.
Here’s an example of how to use NavigableSet
:
import java.util.*; public class NavigableSetExample { public static void main(String[] args) { NavigableSet<Integer> numbers = new TreeSet<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40); numbers.add(50); System.out.println("Navigable set of numbers: " + numbers); System.out.println("Ceiling of 25: " + numbers.ceiling(25)); System.out.println("Floor of 25: " + numbers.floor(25)); System.out.println("Higher of 25: " + numbers.higher(25)); System.out.println("Lower of 25: " + numbers.lower(25)); } }
In this example, we first create a NavigableSet
of Integer
objects called numbers
and add some elements to it using the add()
method. We then print the navigable set of numbers using the toString()
method.
We also perform some navigational operations using the ceiling()
, floor()
, higher()
, and lower()
methods. These methods return the least/greatest element in the set that is greater/less than or equal to a given element. In this example, we pass the element 25
to each of these methods and print the result.
Here are some important methods provided by the NavigableSet
interface:
ceiling(E element)
– Returns the least element in the set that is greater than or equal to the specified element, or null
if there is no such element.floor(E element)
– Returns the greatest element in the set that is less than or equal to the specified element, or null
if there is no such element.higher(E element)
– Returns the least element in the set that is greater than the specified element, or null
if there is no such element.lower(E element)
– Returns the greatest element in the set that is less than the specified element, or null
if there is no such element.descendingSet()
– Returns a NavigableSet
containing the elements of this set in reverse order.pollFirst()
– Retrieves and removes the first (lowest) element, or returns null
if the set is empty.pollLast()
– Retrieves and removes the last (highest) element, or returns null
if the set is empty.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
– Returns a view of the portion of this set whose elements range from the specified fromElement
to the specified toElement
.headSet(E toElement, boolean inclusive)
– Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive
is true
) the specified toElement
.tailSet(E fromElement, boolean inclusive)
– Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive
is true
) the specified fromElement
.These methods can be used to navigate through the NavigableSet
and perform various operations on it. It is important to note that NavigableSet
is an interface, so it cannot be instantiated directly. Instead, you need to create an instance of a class that implements the NavigableSet
interface, such as TreeSet
.
Here’s an example of using some of these methods:
import java.util.*; public class NavigableSetExample { public static void main(String[] args) { NavigableSet<Integer> numbers = new TreeSet<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40); numbers.add(50); System.out.println("Original set: " + numbers); // Find the first and last elements System.out.println("First element: " + numbers.first()); System.out.println("Last element: " + numbers.last()); // Find the ceiling and floor elements System.out.println("Ceiling of 25: " + numbers.ceiling(25)); System.out.println("Floor of 25: " + numbers.floor(25)); // Find the higher and lower elements System.out.println("Higher of 25: " + numbers.higher(25)); System.out.println("Lower of 25: " + numbers.lower(25)); // Create a reverse view of the set NavigableSet<Integer> reverseNumbers = numbers.descendingSet(); System.out.println("Reverse set: " + reverseNumbers); // Remove the first and last elements numbers.pollFirst(); numbers.pollLast(); System.out.println("Set after removing first and last elements: " + numbers); // Get a sub-set of the set NavigableSet<Integer> subSet = numbers.subSet(20, true, 40, false); System.out.println("Sub-set: " + subSet); } }
In this example, we first create a NavigableSet
of Integer
objects called numbers
and add some elements to it using the add()
method. We then print the original set of numbers using the toString()
method.
We perform various navigational operations using the first()
, last()
, ceiling()
, floor()
, higher()
, and lower()
methods, and print the results.
We also create a reverse view of the set using the descendingSet()
method, remove the first and last elements using the pollFirst()
and pollLast()
methods, and get a sub-set of the set using the subSet()
method. We print the results of each of these operations.
These methods provide a powerful way to manipulate and navigate through sets of data in Java.
Other useful methods provided by the NavigableSet
interface include:
descendingIterator()
: Returns an iterator over the elements in the set in descending order.descendingSet()
: Returns a reverse view of the set.headSet(E toElement, boolean inclusive)
: Returns a view of the portion of the set whose elements are strictly less than toElement
(or less than or equal to toElement
if inclusive
is true).tailSet(E fromElement, boolean inclusive)
: Returns a view of the portion of the set whose elements are greater than or equal to fromElement
(or strictly greater than fromElement
if inclusive
is false).pollFirst()
: Removes and returns the first (lowest) element in the set.pollLast()
: Removes and returns the last (highest) element in the set.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
: Returns a view of the portion of the set whose elements range from fromElement
to toElement
, inclusive or exclusive, depending on the values of the fromInclusive
and toInclusive
parameters.These methods allow you to perform a variety of operations on navigable sets, such as iterating over the elements in reverse order, getting subsets of the set based on certain criteria, and removing elements from the beginning or end of the set.
Overall, the NavigableSet
interface provides a powerful way to manipulate and navigate through sets of data in Java, with many useful methods for finding, adding, removing, and navigating elements in the set.