Home Java Collections Methods checkedNavigableSet() Java Collections checkedNavigableSet() Method 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.
NavigableSet<Integer> levels =
Collections.checkedNavigableSet(new TreeSet<Integer>(), Integer.class);
NavigableSet<Integer> down = levels.descendingSet(); // also checked Call What 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.util.Collections (Java 8+)
public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s, Class<E> type) Parameters Parameter Description sThe navigable set to protect. typeThe class every element must be an instance of.
Return Value Return Value Description NavigableSet<E>A dynamically type-safe view of s.
Compare with Similar Methods Related wrappers:
Java Examples 1. Descending view 2. Polling allowed 3. Inclusive subSet Example 1 — The Descending View Is Checked Add through a reverse view: a String fails, an Integer lands in the right place.
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);
}
} add "7": ClassCastException
descending [9, 7, 5, 1], ascending [1, 5, 7, 9] How It Works View inherits the check. descendingSet() of the wrapper is another checked navigable set.Rejected. The text "7" fails the Integer check through the raw reference.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.
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);
}
} ceiling("b") = b-job
pollFirst = a-job, pollLast = c-job
left [b-job] How It Works Lookup. "b" is shorter than "b-job", so it sorts just before it; ceiling("b") returns b-job.Removal is fine. Polling takes elements out, which needs no type check, so a-job and c-job are removed.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.
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);
}
} 2023L: ClassCastException
span [2022, 2023, 2024], years [2020, 2022, 2023, 2024] How It Works Inclusive slice. subSet(2021, true, 2024, true) covers 2022 and 2024, including the upper end.Near miss. 2023L is a Long; same digits, different class, so the check fails.Correct type. The int 2023 boxes to Integer and is added to both the slice and the full set.Frequently Asked Questions What does Collections.checkedNavigableSet() do? It returns a NavigableSet view that type-checks inserted elements while keeping all navigation methods.
Can I call pollFirst() on a checked navigable set? Yes. Removal is allowed; only inserting a wrong type is blocked.
Are descendingSet() and subSet() views checked? Yes. Every view returned by the wrapper is also checked.
Which Java version added checkedNavigableSet()? Java 8.
Why is a Long rejected by an Integer set? The check uses Integer.class.isInstance, and a Long is not an Integer even if the number is the same.
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.
14 people found this page helpful
Helpful Share Copy link Suggestion