Java Collections unmodifiableNavigableSet() Method

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

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.

Java
NavigableSet<Integer> sizes = Collections.unmodifiableNavigableSet(stock);
Integer fit = sizes.ceiling(39);        // smallest size >= 39
NavigableSet<Integer> big = sizes.descendingSet();
CallWhat 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
// java.util.Collections (Java 8+)
public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s)

Parameters

ParameterDescription
sThe navigable set to expose read-only. Must not be null.

Return Value

Return ValueDescription
NavigableSet<T>A read-only view of s.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
unmodifiableSortedSet(s)Older; no ceiling/floor on the result type.
synchronizedNavigableSet(s)Thread-safe, still changeable.
emptyNavigableSet()Read-only navigable set with no elements.

Java Examples

Example 1 — Find the Nearest Shoe Size in Stock

Use ceiling, floor, higher, and lower on a read-only stock list.

Java
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));
    }
}

How It Works

  1. Round up and down. Size 39 is missing, so ceiling suggests 40; for 43, floor suggests 41.
  2. Nothing beyond the ends. No size is above 44 or below 38, so those lookups return null.
  3. 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.

Java
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);
    }
}

How It Works

  1. Peek at both ends. first() and last() only read, so they return t1 and t3.
  2. Poll means remove. pollFirst() would take t1 out of the set, which the read-only view forbids.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Reverse view. descendingSet() shows the same years from newest to oldest, still read-only.
  2. Both ends included. The four-argument subSet lets you include 2025, which the older two-argument form would leave out.
  3. Iterator locked. descendingIterator() reads 2025 first, but its remove() throws.

Frequently Asked Questions

It returns a read-only NavigableSet view that supports ceiling, floor, higher, lower, and descending views.
pollFirst removes the element it returns, and removal is not allowed. Use first() to read the smallest element.
Java 8.
Yes. All sub-views and iterators are read-only as well.
The navigable version returns a NavigableSet, so ceiling, floor, inclusive ranges, and descending views are available.

Next: unmodifiableSet()

Wrap a Set in a read-only view.

unmodifiableSet() 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.

9 people found this page helpful