Open In App

SortedSet Interface in Java

Last Updated : 24 Oct, 2025
Comments
Improve
Suggest changes
25 Likes
Like
Report

The SortedSet interface is present in java.util package that extends the Set interface. which maintains unique elements in sorted order, either by natural ordering or a custom comparator.

  • Elements are maintained in ascending order by default.
  • Ensures that no duplicate elements are present, consistent with the Set interface.
  • Null elements are not allowed, especially if natural ordering or a custom comparator is used.

Declaration Syntax

Since SortedSet is an interface, objects cannot be created directly. Use a class like TreeSet that implements it.

public interface SortedSet<E> extends Set<E>

Java
import java.util.SortedSet;
import java.util.TreeSet;

public class GFG{
    
    public static void main(String[] args){
        
        SortedSet<Integer> set = new TreeSet<>();
        set.add(50);
        set.add(20);
        set.add(40);
        set.add(10);

        System.out.println(set);
        System.out.println("First element: " + set.first());
        System.out.println("Last element: " + set.last());
    }
}

Output
[10, 20, 40, 50]
First element: 10
Last element: 50

Explanation:

  • A TreeSet implements SortedSet.
  • Elements are automatically sorted in ascending order.
  • first() and last() return the smallest and largest elements, respectively.

Hierarchy of SortedSet

SortedSet interface extends the Set interface.

Sorted
SortedSet-Heirarchy

Performing Different Operations on SortedSet

1. Adding Elements

To add elements to a SortedSet, use the add() method.In a TreeSet, elements are automatically sorted in ascending order, duplicates are ignored, and null values are not allowed.

Java
import java.util.*;

class Geeks {

    public static void main(String[] args) 
    {
        SortedSet<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

We can access elements using methods such as contains()first()last(), etc.

Java
import java.util.*;
class Geeks {

    public static void main(String[] args)
    {
        SortedSet<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("Sorted Set is: " + ts);

        String check = "D";

        // Check if the above string exists in the SortedSet or not
        System.out.println("Contains: " + check + " " 
                           + ts.contains(check));

        // Print the first element in the SortedSet
        System.out.println("First Value: " + ts.first());

        // Print the last element in the SortedSet
        System.out.println("Last Value: " + ts.last());
    }
}

Output
Sorted 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 SortedSet using the remove() method.

Java
import java.util.*;
class Geeks {

    public static void main(String[] args)
    {
        SortedSet<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);
    }
}

Output
Initial TreeSet: [A, B, C, D, E]
After removing element: [A, C, D, E]

4. Iterating through the SortedSet

There are various ways to iterate through the SortedSet. The most famous one is to use the enhanced for loop.

Java
import java.util.*;
class Geeks
 { 
    public static void main(String[] args)
    {
        SortedSet<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 SortedSet
        for (String value : ts)
            System.out.print(value
                             + ", ");
        System.out.println();
    }
}

Output
A, B, C, D, E, Z, 

Note: The class which implements the SortedSet interface is TreeSet.

Methods of SortedSet Interface

The following are the methods present in the SortedSet interface. Here, the “*” represents that the methods are part of the Set interface.

MethodDescription
add(element)Adds an element if not already present.
addAll(collection)Adds all elements from another collection.
clear()Removes all elements (set remains empty).
comparator()This method returns the comparator used to order the elements in this set.
contains(element)This method is used to check whether a specific element is present in the Set or not.
containsAll(collection)Checks if all elements of a collection are in the set.
first()This method returns the first(lowest) element present in this set.
hashCode() Returns the hash code of the set.
headSet(element)This method returns the elements which are less than the element that are present in the sorted set.
isEmpty()This method is used to check if a SortedSet is empty or not.
last()This method returns the last(highest) element present in the set.
remove(element)Removes the given element
removeAll(collection)Removes all matching elements from the set.
retainAll(collection)Keeps only elements present in the given collection.
size()Returns the number of elements in the set.
subSet(element1, element2)This method returns a sorted subset from the set containing the elements between element1 and element2.
tailSet(element)This method returns the elements which are greater than or equal to the element that are present in the sorted set.
toArray()This method is used to form an array of the same elements as that of the Set.
Suggested Quiz
3 Questions

What is the primary feature of a SortedSet?

  • A

    Maintains insertion order

  • B

    Stores elements randomly

  • C

    Maintains elements in sorted order

  • D

    Allows duplicates

Explanation:

SortedSet stores elements in ascending sorted order by default.

Which class is the most commonly used implementation of SortedSet?

  • A

    HashSet

  • B

    LinkedHashSet

  • C

    TreeSet

  • D

    EnumSet

Explanation:

TreeSet implements the SortedSet interface.

How does SortedSet handle duplicates?

  • A

    Throws an exception

  • B

    Replaces existing element

  • C

    Stores them as separate entries

  • D

    Ignores duplicate elements

Explanation:

SortedSet follows Set rules, so duplicates are not allowed.

Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Explore