Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Create a NavigableSet
3.
Methods of NavigableSet
3.1.
descendingSet()
3.2.
descendingIterator()
3.3.
headSet()
3.4.
tailSet()
3.5.
subSet()
3.6.
ceiling()
3.7.
floor()
3.8.
higher()
3.9.
lower()
3.10.
pollFirst()
3.11.
pollLast()
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024

NavigableSet

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The Java NavigableSet interface is a subtype of the Java SortedSet interface (java.util.NavigableSet). As a result, the NavigableSet acts similarly to a SortedSet, but with an additional set of navigation capabilities in addition to the SortedSet's sorting operations. In this article, we will go through some of the Java NavigableSet's navigation methods in detail.

In Java 6 to 13, the java.util.TreeSet class is the only implementation of the NavigableSet interface in java.util package. ConcurrentSkipListSet is an implementation in the java.util.concurrent package. However, it is outside the scope of this blog.

Also Read About, Multithreading in java

Create a NavigableSet

To make a Java NavigableSet, you must first build an instance of one of the NavigableSet interface classes. Here's an example of how to make a TreeSet instance that implements the NavigableSet interface:

NavigableSet navigableSet = new TreeSet();

Source: Programiz

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

  1. 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.
     
  2. 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.
     
  3. 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!

Previous article
LinkedHashSet
Next article
ConcurrentSkipListSet
Live masterclass