NavigableSet in Java with Example | How to Create a Java NavigableSet Interface? | Methods & Example Programs

The NavigableSet Interface in java is a subinterface of SortedSet which gives additional methods for navigating the elements in the set. From this tutorial of Java NavigableSet, you can easily & quickly learn all Navigable Set Methods with Sample Program. Also, understand how to create and use a NavigableSet in Java using a class that implements it.

This Tutorial of Navigable Set in Java Includes the following:

Java NavigableSet

NavigableSet in Java is an interface present in java.util package. It is part of the collection framework in java. NavigableSet is the child interface of the SortedSet interface. NavigableSet came in Java 1.6 version. It behaves like a SortedSet exception that defines several methods for navigation purposes. It can navigate the set in reverse order compared to the order defined in the SortedSet.

NavigableSet in Java with Example 1

Also See:

Interface NavigableSet<E>

Type Parameters:

E – the type of elements maintained by this set

All Superinterfaces:

Collection<E>, Iterable<E>, Set<E>, SortedSet<E>

All Known Implementing Classes:

ConcurrentSkipListSet, TreeSet

Java NavigableSet Implementations

In Java, the implementation of the NavigableSet interface can be done by the java.util package ie., java.util.TreeSet class. Also, there is an implementation in the java.util.concurrent package called ConcurrentSkipListSet but that is the outer scope of this path.

Create a NavigableSet

In order to create a Java Navigable Set, you have to make an instance of one of the classes implementing the NavigableSet interface. Following is an instance of creating an instance of the class TreeSet which implements the NavigableSet interface:

NavigableSet navigableSet = new TreeSet();

How to use NavigableSet?

For using the NavigableSet, we need to import the java.util.NavigableSet package in java. After importing the package, here’s how we can create navigable sets.

// SortedSet implementation by TreeSet class
NavigableSet<String> numerals = new TreeSet<>();

In the above example, we have created a navigable set named numerals of the TreeSet class.

Methods of NavigableSet in Java

1. floor(E e): This method returns the highest element in this set which is less than or equal to the given element, it returns null if there is no such element.

2. lower(E e): This method returns the highest element in this set which is less than to the given element, it returns null if there is no such element.

3. ceiling(E e): This method returns the least element in this set which is greater than or equal to the given element, it returns null if there is no such element.

4. higher(E e): This method returns the least element in this set which is greater than the given element, it returns null if there is no such element.

5. pollFirst(E e): This method is used to retrieve and remove the first least element, it returns null if there is no such element.

6. pollLast(E e): This method is used to retrieve and remove the last highest element, it returns null if there is no such element.

7. desecendingSet(): This method is used to returns the navigable set in reverse order.

8. descendingIterator() – This method returns an iterator that can be used to iterate over a set in reverse order.

Java NavigableSet Example

import java.util.NavigableSet;
import java.util.TreeSet;
class Person
{
public static void main(String args[])
{
NavigableSet nset = new TreeSet();

nset.add(100);
nset.add(200);
nset.add(300);
nset.add(400);
nset.add(500);
nset.add(600);
nset.add(700);

//displaying normal order
System.out.println("Normal order of navigable set is: " +nset);

//displaying reverse order
System.out.println("Reverse order of navigable set is: " +nset.descendingSet());

System.out.println("Lower(300) is: " +nset.lower(300));
System.out.println("floor(400) is: " +nset.floor(400));
System.out.println("Ceiling(300) is: " +nset.ceiling(300));
System.out.println("Higher(200) is: " +nset.higher(200));
System.out.println("pool first is: " +nset.pollFirst());
System.out.println("pool last is: " +nset.pollLast());
}
}

Output:
The normal order of navigable set is: [100, 200, 300, 400, 500, 600, 700]
The reverse order of navigable set is: [700, 600, 500, 400, 300, 200, 100]
Lower(300) is: 200
floor(400) is: 400
Ceiling(300) is: 300
Higher(200) is: 300
poll first is: 100
poll last is: 700