Java Collections checkedNavigableMap() Method

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

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.

Java
NavigableMap<Integer, String> floors = Collections.checkedNavigableMap(
    new TreeMap<Integer, String>(), Integer.class, String.class);
String below = floors.floorEntry(4).getValue();
CallWhat 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
// java.util.Collections (Java 8+)
public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K,V> m,
                                                          Class<K> keyType, Class<V> valueType)

Parameters

ParameterDescription
mThe navigable map to protect.
keyTypeThe class every key must belong to.
valueTypeThe class every value must belong to.

Return Value

Return ValueDescription
NavigableMap<K,V>A dynamically type-safe view of m.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
checkedSortedMap(m, k, v)Older; no navigation on the result type.
unmodifiableNavigableMap(m)Blocks every change.
synchronizedNavigableMap(m)Thread-safe navigable map.

Java Examples

Example 1 — Building Floors: Wrong Value, Wrong Key

Raw puts with a numeric name and a text floor number both fail; navigation still works.

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

How It Works

  1. Value check. Floor 5 with the number 55 as its name fails: values must be String.
  2. Key check. The text "7" fails as a key, before the TreeMap would even compare it.
  3. 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.

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

How It Works

  1. Priority as key. Lower numbers are more urgent, so the tree keeps deploy (1) first.
  2. Poll removes. pollFirstEntry() returns and removes 1=deploy; checked maps allow removals.
  3. 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.

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

How It Works

  1. Inclusive, checked view. headMap("kiwi", true) includes kiwi and is itself type-checked.
  2. Bad value. "lots" is not an Integer, so the raw put through the view fails.
  3. 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

It returns a NavigableMap view that type-checks keys and values on insert while keeping all navigation methods.
Yes. Removals are allowed; only wrong-type inserts are blocked.
Yes. Views returned by the wrapper perform the same checks.
Java 8.
The checked map allows correct changes and blocks wrong types; the unmodifiable map blocks every change.

Next: checkedNavigableSet()

Wrap a NavigableSet so inserts are type-checked while navigation and polling still work.

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

13 people found this page helpful