Java Collections checkedSortedMap() Method

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

Definition

In Java, Collections.checkedSortedMap(m, keyType, valueType) returns a SortedMap wrapper that type-checks the key and value of every inserted mapping, while keeping firstKey(), lastKey(), and range views.

A TreeMap compares keys, so it sometimes notices a wrong key type, but it never looks at values. A wrong value type is caught only by the checked wrapper.

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

Syntax

Pass the sorted map, key class, and value class.

Java
SortedMap<String, Double> prices = Collections.checkedSortedMap(
    new TreeMap<String, Double>(), String.class, Double.class);
SortedMap raw = prices;
raw.put("cake", 4);   // ClassCastException: Integer is not Double
CallWhat it does
raw.put(k, v)Key and value checked.
headMap / tailMap / subMapReturn checked sorted maps.
firstKey() / lastKey()Same as the wrapped map.

Added in Java 5. Wrappers can be stacked; for example a synchronized wrapper around a checked one gives both thread safety and type checks.

Library declarations
Java
// java.util.Collections
public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType)

Parameters

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

Return Value

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

Compare with Similar Methods

Related wrappers:

MethodHow it differs
checkedNavigableMap(m, k, v)Adds navigation methods (Java 8).
checkedMap(m, k, v)Any map.
synchronizedSortedMap(m)Thread-safe sorted map.

Java Examples

Example 1 — Integer Is Not Double

A price map expects Double values. A raw put of the literal 4 is refused; 4.0 works.

Java
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionsCheckedSortedMapValue {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        SortedMap<String, Double> prices = Collections.checkedSortedMap(
            new TreeMap<String, Double>(), String.class, Double.class);
        prices.put("tea", 2.5);

        SortedMap raw = prices;
        try {
            raw.put("cake", 4);
        } catch (ClassCastException e) {
            System.out.println("put(cake, 4): " + e.getClass().getSimpleName());
        }
        raw.put("cake", 4.0);
        System.out.println(prices + ", first " + prices.firstKey());
    }
}

How It Works

  1. TreeMap ignores values. Without the wrapper, an Integer value would be stored silently, because the tree only compares keys.
  2. Autoboxing trap. The literal 4 boxes to Integer, which is not a Double, so the check fails.
  3. Fixed. 4.0 boxes to Double; cake is stored and, sorting before tea, becomes firstKey().

Example 2 — A tailMap View Checks Values Too

Write a Character grade through a raw tailMap reference, then the correct String.

Java
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionsCheckedSortedMapTail {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        SortedMap<Integer, String> grades = Collections.checkedSortedMap(
            new TreeMap<Integer, String>(), Integer.class, String.class);
        grades.put(50, "E");
        grades.put(70, "C");
        grades.put(90, "A");

        SortedMap<Integer, String> passing = grades.tailMap(60);
        SortedMap raw = passing;
        try {
            raw.put(80, 'B');
        } catch (ClassCastException e) {
            System.out.println("char grade: " + e.getClass().getSimpleName());
        }
        passing.put(80, "B");
        System.out.println("passing " + passing);
        System.out.println("all " + grades);
    }
}

How It Works

  1. Checked slice. tailMap(60) of the wrapper is itself a checked sorted map with the same key and value types.
  2. Character vs String. 'B' boxes to Character, so the value check fails.
  3. Shared data. The correct "B" added through the slice shows up in the full grade map as well.

Example 3 — Thread-Safe and Type-Safe Together

Wrap a checked sorted map in synchronizedSortedMap to get both behaviors.

Java
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionsCheckedSortedMapStack {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        SortedMap<String, Integer> safe = Collections.synchronizedSortedMap(
            Collections.checkedSortedMap(new TreeMap<String, Integer>(), String.class, Integer.class));
        safe.put("b", 2);
        safe.put("a", 1);

        SortedMap raw = safe;
        try {
            raw.put("c", "3");
        } catch (ClassCastException e) {
            System.out.println("put(c, \"3\"): " + e.getClass().getSimpleName());
        }
        System.out.println(safe + ", lastKey " + safe.lastKey());
    }
}

How It Works

  1. Layers. The outer synchronized wrapper takes a lock, then passes the call to the inner checked wrapper, which checks types before reaching the TreeMap.
  2. Type check still applies. The value "3" is a String, so the inner layer throws.
  3. Sorted reads. The valid entries print in key order and lastKey() is b.

Frequently Asked Questions

It returns a SortedMap view that checks the key and value types of every inserted mapping.
Only keys, indirectly, through comparisons. It never checks value types, which is where the wrapper helps most.
Yes. Sub-maps of the wrapper are checked sorted maps.
The literal 4 autoboxes to Integer. Use 4.0 so it boxes to Double.
Yes. Wrap the checked map in the synchronized one to get both behaviors.

Next: checkedSortedSet()

Wrap a SortedSet so wrong-type elements are rejected, including through range views.

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

8 people found this page helpful