The perfect place for easy learning...

Java Programming


Topics List

Java SortedSet Interface

Set Interface

The Set interface is a child interface of Collection interface. It does not defines any additional methods of it, it has all the methods that are inherited from Collection interface. The Set interface does not allow duplicates. Set is a generic interface.

SortedSet Interface

The SortedSet interface is a child interface of the Set interface. The SortedSet interface is available inside the java.util package. It defines the methods that are used by classes HashSet, LinkedHashSet, and TreeSet.

🔔 The SortedSet interface extends Set interface.

🔔 The SortedSet interface does not allow duplicate elements.

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

The SortedSet interface defines the following methods.

Deque interface in java

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

import java.util.*;

public class SortedSetInterfaceExample {

	public static void main(String[] args) {

		SortedSet sortedSet = new TreeSet();
		System.out.println("\nElements of sortedSet: " + sortedSet);
		System.out.println("\nFirst element: " + sortedSet.first());	
		System.out.println("\nLast element: " + sortedSet.last());	
		System.out.println("\nSubset with upper limit: " + sortedSet.headSet(30));
		System.out.println("\nSubset with lower limit: " + sortedSet.tailSet(30));
		System.out.println("\nSubset with upper and lower limit: " + sortedSet.subSet(10, 30));



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

SortedSet interface in java