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:
- comparator() : Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
- first() : Returns the first (lowest) element currently in this set.
- headSet(E toElement) : Returns a view of the portion of this set whose elements are strictly less than toElement.
- last() : Returns the last (highest) element currently in this set.
- subSet(E fromElement, E toElement) : Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
- 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.
Recommended Posts:
- List Interface in Java with Examples
- SortedMap Interface in Java with Examples
- Java 8 | DoubleSupplier Interface with Examples
- Java 8 | BooleanSupplier Interface with Examples
- ToLongFunction Interface in Java with Examples
- LongToIntFunction Interface in Java with Examples
- ToIntFunction Interface in Java with Examples
- ToDoubleFunction Interface in Java with Examples
- Java 8 | IntSupplier Interface with Examples
- Java 8 | LongSupplier Interface with Examples
- IntConsumer Interface in Java with Examples
- LongConsumer Interface in Java with Examples
- DoubleConsumer Interface in Java with Examples
- Supplier Interface in Java with Examples
- IntFunction Interface in Java with Examples




