Home Java Collections Methods checkedNavigableMap() Java Collections checkedNavigableMap() Method Definition In Java 8 and later, Collections.checkedNavigableMap(m, keyType, valueType) returns a NavigableMap wrapper that type-checks the key and value of every inserted mapping and keeps all navigation methods.
Lookups such as floorKey and ceilingKey and removals such as pollFirstEntry work as usual. Only inserts with a wrong key or value type throw ClassCastException.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.checkedNavigableMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax Pass the navigable map, key class, and value class.
NavigableMap<Integer, String> floors = Collections.checkedNavigableMap(
new TreeMap<Integer, String>(), Integer.class, String.class);
String below = floors.floorEntry(4).getValue(); Call What it does put / putAllKey and value type-checked. headMap(k, true) / descendingMap()Return checked views. pollFirstEntry() / removeAllowed. floorKey / ceilingKey / higherEntryUnchanged lookups.
Added in Java 8 (the NavigableMap interface dates from Java 6). Before Java 8, use checkedSortedMap.
Library declarations // java.util.Collections (Java 8+)
public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K,V> m,
Class<K> keyType, Class<V> valueType) Parameters Parameter Description mThe navigable map to protect. keyTypeThe class every key must belong to. valueTypeThe class every value must belong to.
Return Value Return Value Description NavigableMap<K,V>A dynamically type-safe view of m.
Compare with Similar Methods Related wrappers:
Java Examples 1. Bad key or value 2. Polling allowed 3. Inclusive headMap Example 1 — Building Floors: Wrong Value, Wrong Key Raw puts with a numeric name and a text floor number both fail; navigation still works.
import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsCheckedNavigableMapFloors {
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(String[] args) {
NavigableMap<Integer, String> floors = Collections.checkedNavigableMap(
new TreeMap<Integer, String>(), Integer.class, String.class);
floors.put(0, "lobby");
floors.put(3, "gym");
NavigableMap raw = floors;
try {
raw.put(5, 55);
} catch (ClassCastException e) {
System.out.println("value 55: " + e.getClass().getSimpleName());
}
try {
raw.put("7", "roof");
} catch (ClassCastException e) {
System.out.println("key \"7\": " + e.getClass().getSimpleName());
}
System.out.println(floors + ", floorKey(4) = " + floors.floorKey(4));
}
} value 55: ClassCastException
key "7": ClassCastException
{0=lobby, 3=gym}, floorKey(4) = 3 How It Works Value check. Floor 5 with the number 55 as its name fails: values must be String.Key check. The text "7" fails as a key, before the TreeMap would even compare it.Navigation unaffected. floorKey(4) finds 3, the highest floor at or below 4.Example 2 — A Priority List You Can Still Drain Take the most urgent task with pollFirstEntry and look up the next ones.
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsCheckedNavigableMapPoll {
public static void main(String[] args) {
NavigableMap<Integer, String> tasks = Collections.checkedNavigableMap(
new TreeMap<Integer, String>(), Integer.class, String.class);
tasks.put(2, "email");
tasks.put(1, "deploy");
tasks.put(3, "lunch");
Map.Entry<Integer, String> next = tasks.pollFirstEntry();
System.out.println("next " + next + ", then " + tasks.firstEntry() + ", left " + tasks.size());
System.out.println("ceilingKey(3) = " + tasks.ceilingKey(3));
}
} next 1=deploy, then 2=email, left 2
ceilingKey(3) = 3 How It Works Priority as key. Lower numbers are more urgent, so the tree keeps deploy (1) first.Poll removes. pollFirstEntry() returns and removes 1=deploy; checked maps allow removals.Peek and search. firstEntry() now shows 2=email without removing it, and ceilingKey(3) finds 3 itself.Example 3 — Writing Through an Inclusive headMap A stock view up to and including "kiwi" checks values; the descending key set shows the result.
import java.util.Collections;
import java.util.NavigableMap;
import java.util.TreeMap;
public class CollectionsCheckedNavigableMapHead {
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(String[] args) {
NavigableMap<String, Integer> stock = Collections.checkedNavigableMap(
new TreeMap<String, Integer>(), String.class, Integer.class);
stock.put("apple", 4);
stock.put("kiwi", 0);
stock.put("plum", 9);
NavigableMap<String, Integer> upToKiwi = stock.headMap("kiwi", true);
NavigableMap rawHead = upToKiwi;
try {
rawHead.put("fig", "lots");
} catch (ClassCastException e) {
System.out.println("fig=lots: " + e.getClass().getSimpleName());
}
upToKiwi.put("fig", 2);
System.out.println("up to kiwi " + upToKiwi);
System.out.println("descending keys " + stock.descendingKeySet());
}
} fig=lots: ClassCastException
up to kiwi {apple=4, fig=2, kiwi=0}
descending keys [plum, kiwi, fig, apple] How It Works Inclusive, checked view. headMap("kiwi", true) includes kiwi and is itself type-checked.Bad value. "lots" is not an Integer, so the raw put through the view fails.Good value. fig=2 fits the range (fig sorts before kiwi) and appears in the full map, as the descending key set shows.Frequently Asked Questions What does Collections.checkedNavigableMap() do? It returns a NavigableMap view that type-checks keys and values on insert while keeping all navigation methods.
Can I use pollFirstEntry() on a checked navigable map? Yes. Removals are allowed; only wrong-type inserts are blocked.
Are headMap() and descendingMap() views checked? Yes. Views returned by the wrapper perform the same checks.
Which Java version added checkedNavigableMap()? Java 8.
How is it different from unmodifiableNavigableMap()? The checked map allows correct changes and blocks wrong types; the unmodifiable map blocks every change.
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.
13 people found this page helpful
Helpful Share Copy link Suggestion