Methods of NavigableSet
Given below are the methods present in the NavigableSet interface:
descendingSet()
The descendingSet() method returns a NavigableSet with the elements ordered in the opposite order like this one. The original NavigableSet is backed by the returning "view"; thus, changes to the descending set are reflected in the original set as well.
Here's an easy example:
NavigableSet reverse = original.descendingSet();
descendingIterator()
This method allows you to iterate the items of a NavigableSet (which is also a SortedSet) in reverse order without modifying their internal order.
Iterator reverse = original.descendingIterator();
headSet()
The headSet() method returns a NavigableSet view that only contains elements that are "less than" the specified element. Here's an illustration:
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//this headset will contain "1" and "2"
SortedSet headset = original.headSet("3");
//this headset will contain "1", "2", and "3" because "inclusive"=true
NavigableSet headset = original.headSet("3", true);
tailSet()
The tailSet() method is similar to the headSet() method, with the exception that it returns all elements that are equal to or higher than the specified parameter element. Here's an example of a NavigableSet tailSet() method:
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//this tailSet will contain "2" and "3"
SortedSet tailSet = original.tailSet("2");
//this tailSet will contain "3" only because "inclusive"=false
NavigableSet tailSet = original.tailSet("2", false);
subSet()
You can supply two parameters to the subSet() method to define the boundaries of the view set to return. The elements that match the first boundary are included, but those that match the last boundary are excluded. Here's an example of a Java NavigableSet subSet():
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
original.add("4");
original.add("5");
//this subset will contain "2" and "3"
SortedSet subset = original.subSet("2", "4");
//this subset will contain "2", "3" and "4" because
// fromInclusive=true, and toInclusive=true
NavigableSet subset = original.subSet("2", true, "4", true);
ceiling()
The ceiling() method returns the smallest (least) element in this set that is bigger than or equal to the element put in as a parameter. Here's an example of a Java NavigableSet ceiling():
NavigableSet original = new TreeSet();
original.add("1");
original.add("3");
original.add("5");
//ceiling will be "3".
Object ceiling = original.ceiling("2");
floor()
The floor() method returns the biggest element that is less than or equal to the supplied parameter value, which is the inverse of the ceiling() method. Here's an example of a Java NavigableSet floor() method:
NavigableSet original = new TreeSet();
original.add("1");
original.add("3");
original.add("5");
//floor will be "1".
Object floor = original.floor("2");
higher()
The higher() method returns the lowest (least) element in this set that is bigger than (but not equal to) the element supplied as a parameter. Here's an example of a Java NavigableSet higher() method:
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//higher will be "3".
Object higher = original.higher("2");
lower()
The lower() method returns the highest element that is less than (not equal to) the provided value, which is the inverse of the higher() method. Here's an example of a Java NavigableSet lower() method:
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//lower will be "1"
Object lower = original.lower("2");
pollFirst()
The pollFirst() method returns and removes the NavigableSet's "first" element, or null if it is empty. According to the set's sort order, "first" denotes the smallest element. Here's an example of a Java NavigableSet pollFirst() method:
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//first is "1"
Object first = original.pollFirst();
pollLast()
The pollLast() method returns and deletes the NavigableSet's "last" element. According to the set's element sorting order, "last" signifies "biggest." Here's an example of a Java NavigableSet pollLast() method:
NavigableSet original = new TreeSet();
original.add("1");
original.add("2");
original.add("3");
//last is "3"
Object last = original.pollLast();
Try it on java online compiler.
Frequently Asked Questions
-
What is descendingIterator() used for?
This method is used to iterate the items of a NavigableSet (which is also a SortedSet) in reverse order without modifying their internal order.
-
What is floor() used for?
The floor() method returns the biggest element that is less than or equal to the supplied parameter value, which is the inverse of the ceiling() method.
-
What does ceiling() function return?
The ceiling() method returns the smallest (least) element in this set that is bigger than or equal to the element put in as a parameter.
Conclusion
In this article, we have extensively discussed NavigableSet and its methods.
We hope that this blog has helped you enhance your knowledge regarding NavigableSet. If you want to learn more, check out our articles on Code studio.
Do upvote our blog to help other ninjas grow.
Happy Coding!