English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about the Java NavigableSet interface and its methods through an example.
The NavigableSet interface of the Java Collections framework provides the functionality to navigate between elements in a collection.
It is consideredA type of SortedSet.
To use the features of the NavigableSet interface, we need to use the class NavigableSet implemented by TreeSet.
To use the features of the NavigableSet interface, we need to use the TreeSet class that implements NavigableSet.
// SortedSet implementation of TreeSet class NavigableSet<String> numbers = new TreeSet<>();
Here, we create a navigable set named numbers of the TreeSet class.
NavigableSet is considered a type of SortedSet. This is because NavigableSet inherits the SortedSet interface.
Therefore, all SortedSet methods are also available in NavigableSet. To learn about these methods, please visitJava SortedSet.
However, in NavigableSet, some methods of SortedSet (headSet(), tailSet(), and subSet()) are defined differently.
Let's see how to define these methods in NavigableSet.
The headSet() method returns all elements in the navigable set before the specified element (passed as a parameter).
The booleanValue parameter is optional. The default value is false.
If the value of booleanValue is true, this method returns all elements before the specified element, including the specified element.
tailSet() returns all elements of the navigable set after the specified element (passed as a parameter), including the specified element.
The booleanValue parameter is optional. The default value is true.
If booleanValue is false, this method returns all elements after the specified element, excluding the specified element.
The subSet() method returns e1and e2all elements between, including e1.
bv1and bv2is an optional parameter. bv1The default value is true, bv2The default value is false.
If false is passed as bv1passed, then this method returns e1and e2all elements between, excluding e1.
If true is passed as bv2passed, then this method returns e1and e2all elements between, including e1.
NavigableSet provides various methods that can be used to navigate its elements.
DescendingSet() - Reverse the order of elements in the collection
DescendingIterator() - Return an iterator that can be used to iterate over the collection in reverse order
ceiling() - Return the smallest element greater than or equal to the specified element
floor() - Return the largest element less than or equal to the specified element
Higher() - Return the smallest element greater than the specified element
lower() - Return the largest element less than the specified element
pollFirst() - Return and remove the first element from the collection
pollLast() - Return and remove the last element from the collection
import java.util.NavigableSet; import java.util.TreeSet; class Main { public static void main(String[] args) { //Create a NavigableSet using TreeSet NavigableSet<Integer> numbers = new TreeSet<>(); //Insert an element into the collection 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("The first element: " + firstElement); //Access the last element int lastElement = numbers.last(); System.out.println("The last element: ", + lastElement); //Delete the first element int number1 = numbers.pollFirst(); System.out.println("Delete the first element: ", + number1); //Delete the last element int number2 = numbers.pollLast(); System.out.println("Delete the last element: ", + number2); } }
Output Results
NavigableSet: [1, 2, 3] The first element: 1 The last element: 3 Delete the first element: 1 Delete the last element: 3
For more information on TreeSet, please visitJava TreeSet.
Since we have already known the NavigableSet interface, we will use the TreeSet class to learn its implementation.