Java Collections emptySortedSet() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java 8 and later, Collections.emptySortedSet() returns an empty SortedSet that cannot be changed. It fits methods whose return type is SortedSet, where emptySet() would not compile.

All the SortedSet methods are available: comparator(), the range views headSet, tailSet, and subSet, and first() and last(), which throw because there is no element to return.

A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.emptySortedSet(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Call it with no arguments.

Java
SortedSet<Integer> none = Collections.emptySortedSet();   // []
SortedSet<Integer> head = none.headSet(10);                 // [] (also read-only)
Integer first = none.first();                              // throws NoSuchElementException
CallWhat it does
Collections.emptySortedSet()Immutable empty SortedSet.
set.first() / set.last()Throw NoSuchElementException on it.
set.headSet(x), tailSet(x), subSet(a, b)Empty, read-only range views.
set.comparator()null: natural ordering.

Since Java 8. Before that, code returned Collections.unmodifiableSortedSet(new TreeSet<T>()), which behaves the same but creates new objects each time.

Library declarations
Java
// java.util.Collections
public static <E> SortedSet<E> emptySortedSet()

Parameters

ParameterDescription
NoneemptySortedSet() takes no parameters.

Return Value

Return ValueDescription
SortedSet<E>An empty, immutable sorted set (and its range views).

Compare with Similar Methods

Related empty and sorted collections:

MethodHow it differs
emptySet()Plain Set type; no first() or range views.
emptyNavigableSet()Adds floor, ceiling, and other navigation methods.
unmodifiableSortedSet(s)Read-only view of a real, possibly non-empty sorted set.
emptySortedMap()The same idea for sorted maps.

Java Examples

Example 1 — No First Score in an Empty Sorted Set

A method returns a SortedSet of scores; for a new player it returns an empty one. Guard first() with isEmpty().

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.SortedSet;
import java.util.TreeSet;

public class CollectionsEmptySortedSetFirst {
    static SortedSet<Integer> scores(String player) {
        if (player.equals("max")) {
            return new TreeSet<>(Arrays.asList(70, 55, 91));
        }
        return Collections.emptySortedSet();
    }

    public static void main(String[] args) {
        System.out.println("max lowest: " + scores("max").first());

        SortedSet<Integer> rookie = scores("new");
        System.out.println("rookie: " + rookie + ", comparator " + rookie.comparator());
        try {
            rookie.first();
        } catch (NoSuchElementException e) {
            System.out.println("first(): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Sorted return type. The method promises a SortedSet, so it can return either a TreeSet or emptySortedSet(); emptySet() would not match.
  2. Real data. For "max", first() gives the lowest score, 55.
  3. Natural ordering. The empty sorted set reports comparator() as null, meaning natural order.
  4. Nothing first. On the empty set, first() throws NoSuchElementException, so real code should check isEmpty().

Example 2 — Range Views of an Empty Sorted Set

Take head, tail, and sub views of the empty set, then try to add through one of them.

Java
import java.util.Collections;
import java.util.SortedSet;

public class CollectionsEmptySortedSetRanges {
    public static void main(String[] args) {
        SortedSet<String> none = Collections.emptySortedSet();

        System.out.println("headSet(m): " + none.headSet("m"));
        System.out.println("tailSet(m): " + none.tailSet("m"));
        System.out.println("subSet(a, z): " + none.subSet("a", "z"));

        try {
            none.tailSet("m").add("q");
        } catch (UnsupportedOperationException e) {
            System.out.println("add through view: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Views of nothing. Every range of an empty set is also empty, whatever bounds you choose.
  2. Views stay read-only. The range views are wrapped the same way as the set, so they reject changes too.
  3. No back door. Adding "q" through tailSet("m") throws UnsupportedOperationException.

Example 3 — A Sorted Default for Missing Keys

Each student has a sorted set of grades. A missing student gets an empty sorted set, and the code prints the best grade or "(none)".

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class CollectionsEmptySortedSetDefault {
    public static void main(String[] args) {
        Map<String, SortedSet<Integer>> grades = new TreeMap<>();
        grades.put("lia", new TreeSet<>(Arrays.asList(88, 72, 95)));

        for (String name : Arrays.asList("lia", "omar")) {
            SortedSet<Integer> g = grades.getOrDefault(name, Collections.emptySortedSet());
            String best = g.isEmpty() ? "(none)" : String.valueOf(g.last());
            System.out.println(name + " best: " + best);
        }
    }
}

How It Works

  1. Typed default. The map's values are SortedSet<Integer>, so the default must be a SortedSet too, which emptySortedSet() provides.
  2. Highest is last. For lia, last() returns 95, the largest element of the sorted set.
  3. Guarded call. For omar, isEmpty() is checked first, so last() is never called on the empty set.

Frequently Asked Questions

An empty, immutable SortedSet. It was added in Java 8.
It throws NoSuchElementException, because there is no first element.
emptySet() returns a plain Set. When a method must return SortedSet, you need emptySortedSet().
No. headSet, tailSet, and subSet return empty views that also throw UnsupportedOperationException on changes.
None: comparator() returns null, which means natural ordering.

Next: enumeration()

Get an old-style Enumeration over any modern collection.

enumeration() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful