Java Collections checkedNavigableSet() Method

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

Definition

In Java 8 and later, Collections.checkedNavigableSet(s, type) returns a NavigableSet wrapper that type-checks inserted elements and keeps every navigation method: ceiling, floor, pollFirst, descending and inclusive views.

Unlike an unmodifiable view, it does not block changes. Removing and polling work normally; only inserting a wrong type triggers ClassCastException.

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

Syntax

Pass the navigable set and the element class.

Java
NavigableSet<Integer> levels =
    Collections.checkedNavigableSet(new TreeSet<Integer>(), Integer.class);
NavigableSet<Integer> down = levels.descendingSet();   // also checked
CallWhat it does
add / addAllType-checked.
descendingSet() / subSet(a, true, b, true)Return checked views.
pollFirst() / pollLast() / removeAllowed; no type check needed.
ceiling / floor / higher / lowerUnchanged lookups.

Added in Java 8 (the NavigableSet interface dates from Java 6). On earlier versions, checkedSortedSet is the closest option.

Library declarations
Java
// java.util.Collections (Java 8+)
public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s, Class<E> type)

Parameters

ParameterDescription
sThe navigable set to protect.
typeThe class every element must be an instance of.

Return Value

Return ValueDescription
NavigableSet<E>A dynamically type-safe view of s.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
checkedSortedSet(s, type)Older; no navigation on the result type.
unmodifiableNavigableSet(s)Blocks every change, including polls.
synchronizedNavigableSet(s)Thread-safe navigable set.

Java Examples

Example 1 — The Descending View Is Checked

Add through a reverse view: a String fails, an Integer lands in the right place.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsCheckedNavigableSetDesc {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        NavigableSet<Integer> levels = Collections.checkedNavigableSet(new TreeSet<>(Arrays.asList(1, 5, 9)), Integer.class);
        NavigableSet<Integer> down = levels.descendingSet();

        NavigableSet rawDown = down;
        try {
            rawDown.add("7");
        } catch (ClassCastException e) {
            System.out.println("add \"7\": " + e.getClass().getSimpleName());
        }
        down.add(7);
        System.out.println("descending " + down + ", ascending " + levels);
    }
}

How It Works

  1. View inherits the check. descendingSet() of the wrapper is another checked navigable set.
  2. Rejected. The text "7" fails the Integer check through the raw reference.
  3. One set, two orders. The number 7 added via the reverse view appears in both orders.

Example 2 — Navigation and Polling Work Normally

A checked set is not read-only: ceiling looks up and pollFirst/pollLast remove.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsCheckedNavigableSetPoll {
    public static void main(String[] args) {
        NavigableSet<String> jobs = Collections.checkedNavigableSet(
            new TreeSet<>(Arrays.asList("b-job", "a-job", "c-job")), String.class);

        System.out.println("ceiling(\"b\") = " + jobs.ceiling("b"));
        System.out.println("pollFirst = " + jobs.pollFirst() + ", pollLast = " + jobs.pollLast());
        System.out.println("left " + jobs);
    }
}

How It Works

  1. Lookup. "b" is shorter than "b-job", so it sorts just before it; ceiling("b") returns b-job.
  2. Removal is fine. Polling takes elements out, which needs no type check, so a-job and c-job are removed.
  3. Contrast. On an unmodifiableNavigableSet, both polls would throw; here one job remains.

Example 3 — Long Is Not Integer in an Inclusive Range

Insert into a subSet covering 2021 to 2024, first as a Long, then as an Integer.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsCheckedNavigableSetSpan {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        NavigableSet<Integer> years = Collections.checkedNavigableSet(
            new TreeSet<>(Arrays.asList(2020, 2022, 2024)), Integer.class);
        NavigableSet<Integer> span = years.subSet(2021, true, 2024, true);

        NavigableSet rawSpan = span;
        try {
            rawSpan.add(2023L);
        } catch (ClassCastException e) {
            System.out.println("2023L: " + e.getClass().getSimpleName());
        }
        span.add(2023);
        System.out.println("span " + span + ", years " + years);
    }
}

How It Works

  1. Inclusive slice. subSet(2021, true, 2024, true) covers 2022 and 2024, including the upper end.
  2. Near miss. 2023L is a Long; same digits, different class, so the check fails.
  3. Correct type. The int 2023 boxes to Integer and is added to both the slice and the full set.

Frequently Asked Questions

It returns a NavigableSet view that type-checks inserted elements while keeping all navigation methods.
Yes. Removal is allowed; only inserting a wrong type is blocked.
Yes. Every view returned by the wrapper is also checked.
Java 8.
The check uses Integer.class.isInstance, and a Long is not an Integer even if the number is the same.

Next: checkedQueue()

Wrap a Queue so offer and add reject wrong element types.

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

14 people found this page helpful