The perfect place for easy learning...

Java Programming

×

Topics List


Java NavigableSet Interface





The NavigableSet interface is a child interface of the SortedSet interface. The NavigableSet interface is available inside the java.util package. It defines the methods that are used by class TreeSet.

🔔 The NavigableSet interface extends SortedSet interface.

🔔 The SortedSet interface does not allow duplicate elements.

🔔 The SortedSet interface organise the elements based on the ascending order.

The NavigableSet interface defines several utility methods that are used in the TreeSet class and they are as follows.

NavigableSet interface in java

Let's consider an example program on TreeSet to illustrate the methods of NavigableSet interface.

Example
import java.util.*;

public class NavigableSetInterfaceExample {

	public static void main(String[] args) {

		NavigableSet navSet = new TreeSet();
		
		navSet.add(10);
		navSet.add(20);
		navSet.add(5);
		navSet.add(40);
		navSet.add(30);
		
		System.out.println("\nElements of sortedSet: " + navSet);
		
		System.out.println("\nSmallest element from subSet of larger than 25: " + navSet.ceiling(25));
		
		System.out.println("\nLargest element from subSet of smaller than 25: " + navSet.floor(25));
		
		System.out.println("\nSmallest element from subSet of larger than 25: " + navSet.higher(25));
		
		System.out.println("\nLargest element from subSet of smaller than 25: " + navSet.lower(25));
		
		System.out.println("\nSubset with upperBound, including it: " + navSet.headSet(30, true));
		
		System.out.println("\nSubset with upperBound, excluding it: " + navSet.headSet(30, false));
		
		System.out.println("\nSubset with lowwerBound, including it: " + navSet.tailSet(30, true));
		
		System.out.println("\nSubset with lowerBound, excluding it: " + navSet.tailSet(30, false));
		
		System.out.println("\nRemove the first element: " + navSet.pollFirst());
		
		System.out.println("\nRemove the last element: " + navSet.pollLast());

	}

}

When we run this code, it produce the following output.

NavigableSet interface in java