Collection Framework – The SortedSet Interface

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

Frome 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() - 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() - Returns the first (lowest) element currently in this set.
  • SortedSet headSet(E toElement) - Returns a view of the portion of this set whose elements are strictly less than toElement.
  • E last() - Returns the last (highest) element currently in this set.
  • default Spliterator spliterator() - Creates a Spliterator over the elements in this sorted set.
  • SortedSet subSet(E fromElement, E toElement) - Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
  • SortedSet tailSet(E fromElement) - Returns a view of the portion of this set whose elements are greater than or equal to fromElement.

SortedSet Interface Implementations

SortedSet interface extends Set interface so it inherits all the methods and properties of it. The SortedSet interface can be implemented using all the Set Interface Implementations.
We will learn SortedSet interface with TreeSet implementation class with examples. There is a separate post for TreeSet implementation class.

TreeSet Implementation Class

  1. Overview of TreeSet Class
  2. Create TreeSet Example
  3. TreeSet with a custom comparator (Case Insensitive Order)
  4. Tree Set with a custom Comparator Example(Descending order)
  5. Accessing the elements of a TreeSet
  6. Removing elements from a TreeSet

Learn Guide to TreeSet Class

Comments