NavigableSet in Java with Examples

28 Jun, 2021
NavigableSet represents a navigable set in Java Collection Framework. The NavigableSet interface inherits from the SortedSet interface. It behaves like a SortedSet with the exception that we have navigation methods available in addition to the sorting mechanisms of the SortedSet. For example, the NavigableSet interface can navigate the set in reverse order compared to the order defined in SortedSet. A NavigableSet may be accessed and traversed in either ascending or descending order. The classes that implement this interface are, TreeSet and ConcurrentSkipListSet
Here, E is the type of elements maintained by this set.
All Superinterfaces:
Collection<E>, Iterable<E>, Set<E>, SortedSet<E>
All Known Implementing Classes:
ConcurrentSkipListSet, TreeSet<E>
Declaration: The NavigableSet is declared as
public interface NavigableSet<E>  extends SortedSet<E> 
Creating NavigableSet Objects
Since NavigableSet is an interface, objects cannot be created of the type NavigableSet. We always need a class that extends this list in order to create an object. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the NavigableSet. This type-safe set can be defined as:
// Obj is the type of the object to be stored in NavigableSet
NavigableSet<Obj> set = new TreeSet<Obj> ();
Example:
Java
// Java program to demonstrate// the working of NavigableSetimport java.util.NavigableSet;import java.util.TreeSet; public class NavigableSetDemo{    public static void main(String[] args)    {        NavigableSet<Integer> ns = new TreeSet<>();        ns.add(0);        ns.add(1);        ns.add(2);        ns.add(3);        ns.add(4);        ns.add(5);        ns.add(6);         // Get a reverse view of the navigable set        NavigableSet<Integer> reverseNs = ns.descendingSet();         // Print the normal and reverse views        System.out.println("Normal order: " + ns);        System.out.println("Reverse order: " + reverseNs);         NavigableSet<Integer> threeOrMore = ns.tailSet(3, true);        System.out.println("3 or  more:  " + threeOrMore);        System.out.println("lower(3): " + ns.lower(3));        System.out.println("floor(3): " + ns.floor(3));        System.out.println("higher(3): " + ns.higher(3));        System.out.println("ceiling(3): " + ns.ceiling(3));         System.out.println("pollFirst(): " + ns.pollFirst());        System.out.println("Navigable Set:  " + ns);         System.out.println("pollLast(): " + ns.pollLast());        System.out.println("Navigable Set:  " + ns);         System.out.println("pollFirst(): " + ns.pollFirst());        System.out.println("Navigable Set:  " + ns);         System.out.println("pollFirst(): " + ns.pollFirst());        System.out.println("Navigable Set:  " + ns);         System.out.println("pollFirst(): " + ns.pollFirst());        System.out.println("Navigable Set:  " + ns);         System.out.println("pollFirst(): " + ns.pollFirst());        System.out.println("pollLast(): " + ns.pollLast());    }}
Normal order: [0, 1, 2, 3, 4, 5, 6]
Reverse order: [6, 5, 4, 3, 2, 1, 0]
3 or  more:  [3, 4, 5, 6]
lower(3): 2
floor(3): 3
higher(3): 4
ceiling(3): 3
pollFirst(): 0
Navigable Set:  [1, 2, 3, 4, 5, 6]
pollLast(): 6
Navigable Set:  [1, 2, 3, 4, 5]
pollFirst(): 1
Navigable Set:  [2, 3, 4, 5]
pollFirst(): 2
Navigable Set:  [3, 4, 5]
pollFirst(): 3
Navigable Set:  [4, 5]
pollFirst(): 4
pollLast(): 5



Since NavigableSet is an interface, it can be used only with a class that implements this interface. TreeSet is the class that implements the NavigableSet interface. Now, let’s see how to perform a few frequently used operations on the TreeSet.
1. Adding Elements: In order to add an element to the NavigableSet, we can use the add() method. However, the insertion order is not retained in the TreeSet. Internally, for every element, the values are compared and sorted in the ascending order. We need to keep a note that duplicate elements are not allowed and all the duplicate elements are ignored. And also, Null values are not accepted by the NavigableSet.
Java
// Java code to demonstrate// adding of elements in// NavigableSetimport java.util.*;import java.io.*; class NavigableSetDemo {     public static void main(String[] args)    {        NavigableSet<String> ts = new TreeSet<String>();         // Elements are added using add() method        ts.add("A");        ts.add("B");        ts.add("C");        ts.add("A");         System.out.println(ts);    }}
Output:
[A, B, C]



2. Accessing the Elements: After adding the elements, if we wish to access the elements, we can use inbuilt methods like contains(), first(), last(), etc.
contains()
first()
last()
Java
// Java program to access// the elements of NavigableSetimport java.util.*;import java.io.*; class NavigableSetDemo {     public static void main(String[] args)    {        NavigableSet<String> ts = new TreeSet<String>();         // Elements are added using add() method        ts.add("A");        ts.add("B");        ts.add("C");        ts.add("A");         System.out.println("Navigable Set is " + ts);         String check = "D";         // Check if the above string exists in        // the NavigableSet or not        System.out.println("Contains " + check + " "                           + ts.contains(check));         // Print the first element in        // the NavigableSet        System.out.println("First Value " + ts.first());         // Print the last element in        // the NavigableSet        System.out.println("Last Value " + ts.last());    }}
Output:
Navigable Set is [A, B, C]
Contains D false
First Value A
Last Value C



3. Removing the Values: The values can be removed from the NavigableSet using the remove(), pollFirst(), pollLast() methods.
remove()
pollFirst()
pollLast()
Java
// Java Program to remove the// elements from NavigableSetimport java.util.*;import java.io.*; class NavigableSetDemo {     public static void main(String[] args)    {        NavigableSet<String> ts = new TreeSet<String>();         // Elements are added using add() method        ts.add("A");        ts.add("B");        ts.add("C");        ts.add("B");        ts.add("D");        ts.add("E");         System.out.println("Initial TreeSet " + ts);         // Removing the element b        ts.remove("B");         System.out.println("After removing element " + ts);         // Remove the First element of TreeSet        ts.pollFirst();         System.out.println(            "After the removal of First Element " + ts);         // Remove the Last element of TreeSet        ts.pollLast();         System.out.println(            "After the removal of Last Element " + ts);    }}
Output:
Initial TreeSet [A, B, C, D, E]
After removing element [A, C, D, E]
After the removal of First Element [C, D, E]
After the removal of Last Element [C, D]



4. Iterating through the NavigableSet: There are various ways to iterate through the NavigableSet. The most famous one is to use the enhanced for loop.
Java
// Java program to iterate// through NavigableSet import java.util.*;import java.io.*; class NavigableSetDemo {       public static void main(String[] args)    {        NavigableSet<String> ts = new TreeSet<String>();         // Elements are added using add() method        ts.add("C");        ts.add("D");        ts.add("E");        ts.add("A");        ts.add("B");        ts.add("Z");         // Iterating though the NavigableSet        for (String value : ts)            System.out.print(value + ", ");        System.out.println();    }}
Output:
A, B, C, D, E, Z,  



The following are the methods present in the NavigableSet interface. 
METHOD
DESCRIPTION
subSet​(E fromElement, boolean
fromInclusive, E toElement, boolean toInclusive)
METHOD
DESCRIPTION
METHOD
DESCRIPTION
This method is used to check whether the set contains all the elements present in the given collection or not.
This method returns true if the set contains all the elements and returns false if any of the elements are missing.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Ganeshchowdharysadanala
Java-Collections
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.