Home Java Collections Methods emptyNavigableSet() Java Collections emptyNavigableSet() Method Definition In Java 8 and later, Collections.emptyNavigableSet() returns an empty NavigableSet that cannot be changed. A NavigableSet is a sorted set with "closest match" methods: lower, floor, ceiling, and higher.
On the empty set, those four always return null, because there is never a closest element. The methods that would remove an element, pollFirst and pollLast, throw UnsupportedOperationException, since the set is read-only.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.emptyNavigableSet(...), after import java.util.Collections;. You never create a Collections object.
Syntax Call it with no arguments.
NavigableSet<Integer> none = Collections.emptyNavigableSet();
Integer c = none.ceiling(5); // null
NavigableSet<Integer> rev = none.descendingSet(); // [] (also read-only) Call What it does Collections.emptyNavigableSet()Immutable empty NavigableSet. set.lower(x), floor(x), ceiling(x), higher(x)All return null. set.pollFirst() / pollLast()Throw UnsupportedOperationException (read-only). set.descendingSet()An empty, read-only reversed view.
Since Java 8. Compare with an empty TreeSet, where pollFirst() returns null: the read-only wrapper refuses any removal attempt before even looking at the contents.
Library declarations // java.util.Collections
public static <E> NavigableSet<E> emptyNavigableSet() Parameters Parameter Description None emptyNavigableSet() takes no parameters.
Return Value Return Value Description NavigableSet<E>An empty, immutable navigable set.
Important
first() and last() throw NoSuchElementException; pollFirst(), pollLast(), add, and remove through any view throw UnsupportedOperationException. The four navigation methods return null, so do not unbox their result into an int without a check.
Compare with Similar Methods Related empty and navigable collections:
Java Examples 1. Closest matches 2. pollFirst throws 3. Nearest seat Example 1 — Navigation Methods Return null Ask the empty set for the closest values around 5.
import java.util.Collections;
import java.util.NavigableSet;
public class CollectionsEmptyNavigableSetNav {
public static void main(String[] args) {
NavigableSet<Integer> none = Collections.emptyNavigableSet();
System.out.println("lower(5): " + none.lower(5));
System.out.println("floor(5): " + none.floor(5));
System.out.println("ceiling(5): " + none.ceiling(5));
System.out.println("higher(5): " + none.higher(5));
}
} lower(5): null
floor(5): null
ceiling(5): null
higher(5): null How It Works Strictly below and at-or-below. lower and floor look for the largest element under (or equal to) 5 and find nothing.At-or-above and strictly above. ceiling and higher search upward and also find nothing.null means "no such element". Unlike first(), these methods report a miss with null instead of throwing.Example 2 — pollFirst(): Read-Only Wrapper vs Empty TreeSet Compare pollFirst() on a changeable empty TreeSet and on emptyNavigableSet().
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CollectionsEmptyNavigableSetPoll {
public static void main(String[] args) {
NavigableSet<String> tree = new TreeSet<>();
System.out.println("TreeSet pollFirst: " + tree.pollFirst());
NavigableSet<String> none = Collections.emptyNavigableSet();
try {
none.pollFirst();
} catch (UnsupportedOperationException e) {
System.out.println("emptyNavigableSet pollFirst: " + e.getClass().getSimpleName());
}
System.out.println("descendingSet: " + none.descendingSet());
}
} TreeSet pollFirst: null
emptyNavigableSet pollFirst: UnsupportedOperationException
descendingSet: [] How It Works Changeable set. An empty TreeSet answers pollFirst() with null: nothing to remove.Read-only set. pollFirst is a removal method, and the immutable set rejects it with UnsupportedOperationException even though it is empty.Reverse view. descendingSet() of an empty set is an empty, equally read-only view.Example 3 — A Nearest-Value Lookup That Handles No Data A helper finds the first free seat at or after a requested number. With no free seats, it receives the empty navigable set.
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CollectionsEmptyNavigableSetSeat {
static String seatFrom(NavigableSet<Integer> free, int wanted) {
Integer seat = free.ceiling(wanted);
return seat == null ? "no seat" : "seat " + seat;
}
public static void main(String[] args) {
NavigableSet<Integer> someFree = new TreeSet<>(Arrays.asList(10, 20, 30));
System.out.println(seatFrom(someFree, 15));
System.out.println(seatFrom(someFree, 31));
System.out.println(seatFrom(Collections.emptyNavigableSet(), 15));
}
} How It Works Next free seat. ceiling(15) returns the smallest free seat that is 15 or more: 20.Past the end. No free seat is 31 or more, so ceiling returns null and the helper says "no seat".No data at all. Passing emptyNavigableSet() follows the same null path, so the helper needs no special case for "fully booked".Frequently Asked Questions What does Collections.emptyNavigableSet() return? An empty, immutable NavigableSet. It was added in Java 8.
What do ceiling() and floor() return on an empty navigable set? null, as do lower() and higher().
Why does pollFirst() throw instead of returning null? pollFirst removes an element, and the set is read-only, so it throws UnsupportedOperationException. A changeable empty TreeSet would return null.
What does first() do on emptyNavigableSet()? It throws NoSuchElementException, like first() on any empty sorted set.
Is descendingSet() of an empty navigable set modifiable? No. It is an empty view that is read-only as well.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion