Last Updated: 16 July, 2023
In Java, NavigableSet is an interface that extends the SortedSet interface and provides additional navigation methods for accessing elements in a sorted set. It is part of the Java Collections Framework and is available from Java 6 onward.
A NavigableSet maintains a sorted order of its elements, and it allows efficient retrieval of elements based on their relative position in the set. It provides methods to navigate through the set in both ascending and descending order, perform range queries, and find closest matches for a given value.
NavigableSet declaration:
Here, E defines the type of elements that the Set will contain.
The NavigableSet interface is implemented by the TreeSet class in the Java Collections Framework. TreeSet provides a sorted set implementation based on a binary search tree, which allows efficient navigation and retrieval operations.
Here's an example of using NavigableSet with TreeSet:
Output
Set: [10, 20, 30, 40, 50]
lower(30): 20
floor(35): 30
ceiling(35): 40
higher(30): 40
pollFirst(): 10
pollLast(): 50
Set after poll operations: [20, 30, 40]
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/NavigableSet
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com