Collection Framework – The SortedSet Interface

In this guide, we will learn SortedSet interface, SortedSet interface methods and SortedSet interface implementation class.

Overview of SortedSet Interface

A SortedSet is a Set that maintains its elements in ascending order, sorted according to the natural ordering or according to a Comparator provided at SortedSet creation time. 
In addition to the normal Set operations, the SortedSet interface provides operations for the following:
  • Range view — allows arbitrary range operations on the sorted set
  • Endpoints — returns the first or last element in the sorted set
  • Comparator access — returns the Comparator, if any, used to sort the set
The code for the SortedSet interface follows.
public interface SortedSet<E> extends Set<E> {
    // Range-view
    SortedSet<E> subSet(E fromElement, E toElement);
    SortedSet<E> headSet(E toElement);
    SortedSet<E> tailSet(E fromElement);

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

    // Comparator access
    Comparator<? super E> comparator();
}

SortedSet Interface Example

This example demonstrates the few API of SortedSet Interface using TreeSet implementation class.
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

public class CreateTreeSetExample {
    public static void main(String[] args) {
         // Creating a TreeSet
         SortedSet<String> fruits = new TreeSet<>();

         // Adding new elements to a TreeSet
         fruits.add("Banana");
         fruits.add("Apple");
         fruits.add("Pineapple");
         fruits.add("Orange");

         // Returns the first (lowest) element currently in this set.
         String first = fruits.first();
         System.out.println("First element : " + first);

         // Returns the last (highest) element currently in this set.
         String last = fruits.last();
         System.out.println("Last element : " + last);

         // Returns the comparator used to order the elements in this set, or
         // null if this set uses the natural ordering of its elements.
         Comparator<?> comparator = fruits.comparator();

         SortedSet<String> tailSet = fruits.tailSet("Orange");
         System.out.println("tailSet :" + tailSet);
    }
}
Output:
First element : Apple
Last element : Pineapple
tailSet :[Orange, Pineapple]

SortedSet Interface Hierarchy Diagram

From above diagram, SortedSet interface extends Set interface so it inherit all the methods from all the interface hierarchy. 

SortedSet Interface APIs/Methods

  • Comparator<? super E> comparator() - This method returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
  • E first() - This method returns the first (lowest) element currently in this set.
  • SortedSet headSet(E toElement) - This method returns a view of the portion of this set whose elements are strictly less than toElement.
  • E last() - This method returns the last (highest) element currently in this set.
  • default Spliterator spliterator() - This method creates a Spliterator over the elements in this sorted set.
  • SortedSet subSet(E fromElement, E toElement) - This method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
  • SortedSet tailSet(E fromElement) - This method  returns a view of the portion of this set whose elements are greater than or equal to fromElement.

SortedSet Interface Implementation Class

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course