SortedSet Interface in Java

This tutorial will learn about the SortedSet Interface in Java and its implementation classes and usage.

SortedSet interface in Java is present in java.util package and extends Set Interface, which represents a set of unique objects also adds a feature that stores all the elements in a sorted manner.

Set interface in Java is a subtype of the Java Collection, which means it inherits properties of the collection framework as well.

How to Declare SortedSet in Java?

Let’s see how to create SortedSet in Java:

SortedSet<T> animals = new TreeSet<>();

Here, “T” indicates the type of a SortedSet. For example,

// SortedSet implementation by TreeSet class 
SortedSet<String> students = new TreeSet<>();

SortedSet Interface Declaration

Below is the exact code of the SortedSet Interface present in JDK. You can see what methods SortedSet Interface in Java provides.

package java.util;

import java.util.Spliterators.IteratorSpliterator;

public interface SortedSet<E> extends Set<E> {
    Comparator<? super E> comparator();

    SortedSet<E> subSet(E var1, E var2);

    SortedSet<E> headSet(E var1);

    SortedSet<E> tailSet(E var1);

    E first();

    E last();

    default Spliterator<E> spliterator() {
        return new IteratorSpliterator<E>(this, 21) {
            public Comparator<? super E> getComparator() {
                return SortedSet.this.comparator();
            }
        };
    }
}

Any of its implementation classes will leverage these methods (along with Set Interface methods).

SortedSet Methods:

The SortedSet interface includes all the methods of the Set interface. It’s because Set Interface is a super interface of SortedSet.

MethodDescription
comparator()Returns the comparator which is used to order the elements in the given set.
subSet(E var1, E var2)Returns a portion of the set in the range between var1, inclusive to var2. Also, returns null if the map is empty.
headSet(E var1)Returns a portion of the set, elements less than the var1.
tailSet(E var1)Returns a portion of the set, elements greater than or equal to var1
first()Returns the first element from the set.
last()Returns the last element from the set.
spliterator()Returns a Spliterator that can be used to separate elements. Also, returns null if the map is empty.

SortedSet Example:

Below is a sample program of SortedSet in Java, using TreeSet.

import java.util.SortedSet;
import java.util.TreeSet;

public class SortedSetDemo {
    // Main Method
    public static void main(String[] args) {
        SortedSet<String> sortedSet = new TreeSet();

        // Adding element to Sorted Set
        sortedSet.add("Apple");
        sortedSet.add("Banana");
        sortedSet.add("Mango");
        sortedSet.add("Grapes");

        System.out.println("Printing Fruits: " + sortedSet);

        System.out.println("First Element: " + sortedSet.first());

        System.out.println("Last Element: " + sortedSet.last());

    }
}

Output:

Printing Fruits: [Apple, Banana, Grapes, Mango]
First Element: Apple
Last Element: Mango

Official Docs: Read Here

LinkedHashSet in Java

TreeSet in Java