SortedSet Interface in Java with Examples

SortedSet is an interface in collection framework. This interface extends Set and provides a total ordering of its elements. Exampled class that implements this interface is TreeSet.


All elements of a SortedSet must implement the Comparable interface (or be accepted by the specified Comparator) and all such elements must be mutually comparable (i.e, Mutually Comparable simply means that two objects accept each other as the argument to their compareTo method)

Methods of Sorted Set interface:

  1. comparator() : Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
  2. first() : Returns the first (lowest) element currently in this set.
  3. headSet(E toElement) : Returns a view of the portion of this set whose elements are strictly less than toElement.
  4. last() : Returns the last (highest) element currently in this set.
  5. subSet(E fromElement, E toElement) : Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
  6. tailSet(E fromElement) : Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
public interface SortedSet extends Set           
    // Range views          
    SortedSet subSet(E fromElement, E toElement);    
    SortedSet headSet(E toElement);          
    SortedSet tailSet(E fromElement);          

    // Endpoints          
    E first();          
    E last();          

    // Comparator access    
    Comparator comparator();    
// A Java program to demonstrate working of SortedSet
import java.util.SortedSet;
import java.util.TreeSet;

public class Main
    public static void main(String[] args)
        // Create a TreeSet and inserting elements
        SortedSet<String> sites = new TreeSet<>();

        System.out.println("Sorted Set: " + sites);
        System.out.println("First: " + sites.first());
        System.out.println("Last: " + sites.last());

        // Getting elements before quiz (Excluding) in a sortedSet
        SortedSet<String> beforeQuiz = sites.headSet("quiz");

        // Getting elements between code (Including) and 
        // practice (Excluding)
        SortedSet<String> betweenCodeAndQuiz =

        // Getting elements after code (Including)
        SortedSet<String> afterCode = sites.tailSet("code");


Sorted Set: [code 1="geeksforgeeks," 2="practice," 3="quiz" language=","]
First: code
Last: quiz
[code, geeksforgeeks, practice]
[code, geeksforgeeks ]
[code, geeksforgeeks, practice, quiz]


This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

My Personal Notes arrow_drop_up

Practice Tags :
Article Tags :
Please write to us at to report any issue with the above content.

Recommended Posts:

2 Average Difficulty : 2/5.0
Based on 7 vote(s)

User Actions