Home Java Collections Methods unmodifiableNavigableSet() Java Collections unmodifiableNavigableSet() Method Definition In Java 8 and later, Collections.unmodifiableNavigableSet(s) returns a read-only NavigableSet view. The search methods ceiling, floor, higher, and lower work, as do descending and inclusive range views.
Methods that remove while reading, pollFirst() and pollLast(), count as changes, so they throw UnsupportedOperationException even though first() and last() work.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.unmodifiableNavigableSet(...), after import java.util.Collections;. You never create a Collections object.
Syntax Wrap a TreeSet or another NavigableSet.
NavigableSet<Integer> sizes = Collections.unmodifiableNavigableSet(stock);
Integer fit = sizes.ceiling(39); // smallest size >= 39
NavigableSet<Integer> big = sizes.descendingSet(); Call What it does ceiling(x) / floor(x)Closest element >= / <= x, or null. higher(x) / lower(x)Closest element strictly above / below x. descendingSet()Read-only reverse-order view. pollFirst() / pollLast()Throw UnsupportedOperationException.
Added in Java 8. The NavigableSet interface itself dates from Java 6; on Java 6 or 7 you can only wrap it as unmodifiableSortedSet.
Library declarations // java.util.Collections (Java 8+)
public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) Parameters Parameter Description sThe navigable set to expose read-only. Must not be null.
Return Value Return Value Description NavigableSet<T>A read-only view of s.
Important
add, remove, pollFirst, pollLast, and removals through any iterator, including descendingIterator(), throw UnsupportedOperationException. Every sub-view is read-only too.
Compare with Similar Methods Related wrappers:
Java Examples 1. Nearest match 2. first vs pollFirst 3. Descending views Example 1 — Find the Nearest Shoe Size in Stock Use ceiling, floor, higher, and lower on a read-only stock list.
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CollectionsUnmodifiableNavigableSetSizes {
public static void main(String[] args) {
NavigableSet<Integer> sizes =
Collections.unmodifiableNavigableSet(new TreeSet<>(Arrays.asList(38, 40, 41, 44)));
System.out.println("ceiling(39)=" + sizes.ceiling(39) + ", floor(43)=" + sizes.floor(43));
System.out.println("higher(44)=" + sizes.higher(44) + ", lower(38)=" + sizes.lower(38));
System.out.println("ceiling(41)=" + sizes.ceiling(41) + ", higher(41)=" + sizes.higher(41));
}
} ceiling(39)=40, floor(43)=41
higher(44)=null, lower(38)=null
ceiling(41)=41, higher(41)=44 How It Works Round up and down. Size 39 is missing, so ceiling suggests 40; for 43, floor suggests 41.Nothing beyond the ends. No size is above 44 or below 38, so those lookups return null.Inclusive vs strict. ceiling(41) may return 41 itself; higher(41) must go above it, to 44.Example 2 — first() Works, pollFirst() Does Not Both return the smallest element, but only one of them removes it.
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CollectionsUnmodifiableNavigableSetPoll {
public static void main(String[] args) {
NavigableSet<String> tickets =
Collections.unmodifiableNavigableSet(new TreeSet<>(Arrays.asList("t2", "t1", "t3")));
System.out.println("first(): " + tickets.first() + ", last(): " + tickets.last());
try {
tickets.pollFirst();
} catch (UnsupportedOperationException e) {
System.out.println("pollFirst(): " + e.getClass().getSimpleName());
}
System.out.println("still " + tickets);
}
} first(): t1, last(): t3
pollFirst(): UnsupportedOperationException
still [t1, t2, t3] How It Works Peek at both ends. first() and last() only read, so they return t1 and t3.Poll means remove. pollFirst() would take t1 out of the set, which the read-only view forbids.Nothing lost. After the failed poll, all three tickets are still there.Example 3 — Newest First and Inclusive Ranges Read a set of years backwards, take an inclusive range, and try to remove through the descending iterator.
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CollectionsUnmodifiableNavigableSetDesc {
public static void main(String[] args) {
NavigableSet<Integer> years =
Collections.unmodifiableNavigableSet(new TreeSet<>(Arrays.asList(2019, 2021, 2023, 2025)));
System.out.println("newest first: " + years.descendingSet());
System.out.println("2021..2025: " + years.subSet(2021, true, 2025, true));
Iterator<Integer> it = years.descendingIterator();
System.out.println("iterator starts at " + it.next());
try {
it.remove();
} catch (UnsupportedOperationException e) {
System.out.println("remove: " + e.getClass().getSimpleName());
}
}
} newest first: [2025, 2023, 2021, 2019]
2021..2025: [2021, 2023, 2025]
iterator starts at 2025
remove: UnsupportedOperationException How It Works Reverse view. descendingSet() shows the same years from newest to oldest, still read-only.Both ends included. The four-argument subSet lets you include 2025, which the older two-argument form would leave out.Iterator locked. descendingIterator() reads 2025 first, but its remove() throws.Frequently Asked Questions What does Collections.unmodifiableNavigableSet() do? It returns a read-only NavigableSet view that supports ceiling, floor, higher, lower, and descending views.
Why does pollFirst() throw on the view? pollFirst removes the element it returns, and removal is not allowed. Use first() to read the smallest element.
Which Java version added unmodifiableNavigableSet()? Java 8.
Is descendingSet() of the view read-only? Yes. All sub-views and iterators are read-only as well.
What is the difference from unmodifiableSortedSet()? The navigable version returns a NavigableSet, so ceiling, floor, inclusive ranges, and descending views are available.
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