Home Java Collections Methods emptySortedSet() Java Collections emptySortedSet() Method 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.
SortedSet<Integer> none = Collections.emptySortedSet(); // []
SortedSet<Integer> head = none.headSet(10); // [] (also read-only)
Integer first = none.first(); // throws NoSuchElementException Call What 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.util.Collections
public static <E> SortedSet<E> emptySortedSet() Parameters Parameter Description None emptySortedSet() takes no parameters.
Return Value Return Value Description SortedSet<E>An empty, immutable sorted set (and its range views).
Important
first() and last() throw NoSuchElementException, so check isEmpty() before calling them. Any change, including through a range view, throws UnsupportedOperationException.
Compare with Similar Methods Related empty and sorted collections:
Java Examples 1. first() throws 2. Range views 3. Map default 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().
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());
}
}
} max lowest: 55
rookie: [], comparator null
first(): NoSuchElementException How It Works Sorted return type. The method promises a SortedSet, so it can return either a TreeSet or emptySortedSet(); emptySet() would not match.Real data. For "max", first() gives the lowest score, 55.Natural ordering. The empty sorted set reports comparator() as null, meaning natural order.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.
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());
}
}
} headSet(m): []
tailSet(m): []
subSet(a, z): []
add through view: UnsupportedOperationException How It Works Views of nothing. Every range of an empty set is also empty, whatever bounds you choose.Views stay read-only. The range views are wrapped the same way as the set, so they reject changes too.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)".
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);
}
}
} lia best: 95
omar best: (none) How It Works Typed default. The map's values are SortedSet<Integer>, so the default must be a SortedSet too, which emptySortedSet() provides.Highest is last. For lia, last() returns 95, the largest element of the sorted set.Guarded call. For omar, isEmpty() is checked first, so last() is never called on the empty set.Frequently Asked Questions What does Collections.emptySortedSet() return? An empty, immutable SortedSet. It was added in Java 8.
What happens if I call first() on emptySortedSet()? It throws NoSuchElementException, because there is no first element.
Why not just use Collections.emptySet()? emptySet() returns a plain Set. When a method must return SortedSet, you need emptySortedSet().
Are the range views of emptySortedSet() modifiable? No. headSet, tailSet, and subSet return empty views that also throw UnsupportedOperationException on changes.
What comparator does emptySortedSet() use? None: comparator() returns null, which means natural ordering.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion