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.

sortedsetjava

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<>();
        sites.add("practice");
        sites.add("geeksforgeeks");
        sites.add("quiz");
        sites.add("code");
  
        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");
        System.out.println(beforeQuiz);
  
        // Getting elements between code (Including) and 
        // practice (Excluding)
        SortedSet<String> betweenCodeAndQuiz =
                                  sites.subSet("code","practice");
        System.out.println(betweenCodeAndQuiz);
  
        // Getting elements after code (Including)
        SortedSet<String> afterCode = sites.tailSet("code");
        System.out.println(afterCode);
    }
}

Output:



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

Reference:
http://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-set.html

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


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.