Java Navigable Set Interface
What is Java Navigable Set Interface?
The NavigableSet interface in Java extends the SortedSet interface and provides methods for navigating the set in both forward and backward directions.
It supports efficient retrieval of elements based on their rank, and allows for insertion and removal of elements at the beginning and end of the set. It also provides methods for finding elements closest to a given value.
To understand the Java Navigable Set Interface , Read the Complete Article.
Class that implements NavigableSet
- TreeSet: This class is a sorted set implementation that uses a tree data structure to store elements. It provides O(log n) time complexity for basic operations like add, remove, and contains.
- SubSet: This class is a view of a NavigableSet that represents a subset of the elements in the set. It is created using the subSet method of a NavigableSet implementation and provides a view of the subset without copying the elements
- NavigableSet: This class is an immutable implementation of NavigableSet that does not allow modification of the set after it is created. It is created using the copyOf method of the java.util.Set interface, and provides a thread-safe implementation of NavigableSet.
Advantages of Java Navigable Set Interface:
Efficient navigation:
The NavigableSet interface provides methods for efficiently navigating the set in both forward and backward directions, allowing for easy iteration and traversal.
Rank-based retrieval:
The interface provides methods for retrieving elements based on their rank, which can be more efficient than searching for an element by value.
Range-based operations:
NavigableSet provides methods for performing operations on a range of elements in the set, such as retrieving all elements between two values.
Tailored for specific use cases:
NavigableSet is designed for specific use cases where elements need to be sorted and accessed in a specific order, making it ideal for applications such as data analysis and sorting algorithms.
Example 1: Implementation of NavigableSet in TreeSet Class
Run
import java.util.*; public class Main { public static void main(String[] args) { // Create a new TreeSet NavigableSet treeSet = new TreeSet<>(); // Add some elements to the set treeSet.add(10); treeSet.add(20); treeSet.add(30); treeSet.add(40); // Use NavigableSet methods to navigate the set System.out.println("Floor of 25: " + treeSet.floor(25)); System.out.println("Ceiling of 35: " + treeSet.ceiling(35)); System.out.println("Subset between 15 and 35: " + treeSet.subSet(15, 35)); // Iterate over the set in reverse order System.out.print("Descending order: "); Iterator it = treeSet.descendingIterator(); while (it.hasNext()) { System.out.print(it.next() + " "); } System.out.println(); // Remove an element from the set treeSet.remove(20); // Check if an element exists in the set System.out.println("Contains 20: " + treeSet.contains(20)); // Clear the set treeSet.clear(); } }
Output
Floor of 25: 20 Ceiling of 35: 40 Subset between 15 and 35: [20, 30] Descending order: 40 30 20 10 Contains 20: false
Explanation:
This example creates a new TreeSet, adds some elements to it, and then uses the NavigableSet methods to navigate the set. It also demonstrates how to remove an element, check if an element exists in the set, and clear the set.
Example 2 : Navigable Methods Used.
import java.util.*; //NavigableSetExample public class Main { public static void main(String[] args) { // Create a new TreeSet NavigableSet treeSet = new TreeSet<>(); // Add some elements to the set treeSet.add(10); treeSet.add(20); treeSet.add(30); treeSet.add(40); // Use NavigableSet methods to navigate the set System.out.println("Floor of 25: " + treeSet.floor(25)); System.out.println("Ceiling of 35: " + treeSet.ceiling(35)); System.out.println("Lower of 25: " + treeSet.lower(25)); System.out.println("Higher of 35: " + treeSet.higher(35)); System.out.println("First element: " + treeSet.first()); System.out.println("Last element: " + treeSet.last()); System.out.println("Subset between 15 and 35: " + treeSet.subSet(15, true, 35, true)); System.out.println("Head set with 30: " + treeSet.headSet(30, true)); System.out.println("Tail set with 20: " + treeSet.tailSet(20, true)); System.out.println("Descending order: " + treeSet.descendingSet()); // Remove an element from the set treeSet.remove(20); // Check if an element exists in the set System.out.println("Contains 20: " + treeSet.contains(20)); // Clear the set treeSet.clear(); } }
Output
Floor of 25: 20 Ceiling of 35: 40 Lower of 25: 20 Higher of 35: 40 First element: 10 Last element: 40 Subset between 15 and 35: [20, 30] Head set with 30: [10, 20, 30] Tail set with 20: [20, 30, 40] Descending order: [40, 30, 20, 10] Contains 20: false
Explanation:
This program creates a TreeSet, adds some elements to it, and then uses all of the navigable methods to navigate and manipulate the set. It demonstrates how to use the floor, ceiling, lower, higher, first, and last methods to find elements in the set based on their values. It also shows how to use the subSet, headSet, and tailSet methods to create subsets of the set based on a range of values, and how to use the descendingSet method to iterate over the set in reverse order.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment