Java Navigable Set Interface

Navigable set

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: 

Navigable Set Interface

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

Example 2 : Navigable Methods Used. 

Run

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription