Java Collections checkedCollection() Method

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

Definition

Java generics are checked only at compile time. Through a raw reference or an unchecked cast, an Integer can slip into a Collection<String>, and nothing complains until some later code reads it as a String.

Collections.checkedCollection(c, type) returns a wrapper that checks every inserted element at runtime with type.isInstance. A bad element triggers ClassCastException right at the faulty add, which makes the bug easy to find.

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

Syntax

Pass the collection and the element's Class object.

Java
Collection<String> names =
    Collections.checkedCollection(new ArrayList<String>(), String.class);
Collection raw = names;
raw.add(42);   // ClassCastException right here
CallWhat it does
checkedCollection(c, String.class)Only String (or null) may be added.
checkedCollection(c, Number.class)Subclasses such as Integer and Double are accepted.
checked.addAll(other)Every element is type-checked before any is added.
checked.remove(x) / clear()Removals are not restricted.

Added in Java 5, together with generics. The JDK documentation suggests it as a debugging aid: wrap a collection temporarily to find the code that inserts a wrong type.

Library declarations
Java
// java.util.Collections
public static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type)

Parameters

ParameterDescription
cThe collection to protect. Use only the wrapper afterwards.
typeThe element class every inserted element must be an instance of.

Return Value

Return ValueDescription
Collection<E>A dynamically type-safe view of c.

Compare with Similar Methods

Related type-checked wrappers:

MethodHow it differs
checkedList(l, type)Also checks set and list-iterator writes.
checkedSet(s, type)Type-checked set.
unmodifiableCollection(c)Blocks all writes instead of checking them.

Java Examples

Example 1 — Fail at the Insert, Not Ten Lines Later

A legacy method with a raw parameter sneaks an Integer into a collection of strings. Compare a plain and a checked collection.

Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class CollectionsCheckedCollectionEarly {
    @SuppressWarnings({"unchecked", "rawtypes"})
    static void legacyAdd(Collection c) {
        c.add(Integer.valueOf(42));
    }

    public static void main(String[] args) {
        Collection<String> plain = new ArrayList<>();
        legacyAdd(plain);
        System.out.println("plain accepted it, size " + plain.size());
        try {
            for (String s : plain) {
                System.out.println(s.length());
            }
        } catch (ClassCastException e) {
            System.out.println("plain fails when read: " + e.getClass().getSimpleName());
        }

        Collection<String> checked = Collections.checkedCollection(new ArrayList<String>(), String.class);
        try {
            legacyAdd(checked);
        } catch (ClassCastException e) {
            System.out.println("checked fails at insert: " + e.getClass().getSimpleName() + ", size " + checked.size());
        }
    }
}

How It Works

  1. Raw back door. legacyAdd takes a raw Collection, so the compiler cannot stop it adding 42 to a collection of strings.
  2. Delayed failure. The plain list stores the Integer; the error only appears in the loop, where Java casts each element to String. In a real program that could be far from the bug.
  3. Immediate failure. The checked wrapper tests the element inside add, throws at the guilty line, and stores nothing.

Example 2 — A Mixed addAll Adds Nothing

Add a raw list containing one bad element. The wrapper rejects the whole batch.

Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class CollectionsCheckedCollectionAddAll {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        Collection<String> names = Collections.checkedCollection(new ArrayList<String>(), String.class);
        names.add("ana");

        List mixed = new ArrayList();
        mixed.add("ben");
        mixed.add(7);
        mixed.add("cy");
        try {
            names.addAll(mixed);
        } catch (ClassCastException e) {
            System.out.println("addAll: " + e.getClass().getSimpleName());
        }
        System.out.println("names: " + names);
    }
}

How It Works

  1. Bad batch. The raw list holds two strings and the number 7.
  2. Checked first. In current JDKs the wrapper copies and type-checks every element before handing them to the real list, so 7 is caught up front.
  3. All or nothing. Neither "ben" nor "cy" is added; names still holds only "ana".

Example 3 — Subclasses and null Are Accepted

A collection checked against Number takes any kind of number, plus null.

Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class CollectionsCheckedCollectionSubtypes {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        Collection<Number> nums = Collections.checkedCollection(new ArrayList<Number>(), Number.class);
        nums.add(1);
        nums.add(2.5);
        nums.add(null);
        System.out.println(nums);

        Collection raw = nums;
        try {
            raw.add("3");
        } catch (ClassCastException e) {
            System.out.println("String rejected: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. isInstance test. Integer and Double are both subclasses of Number, so both pass.
  2. null passes. The type check skips null; ArrayList allows it, so it is stored.
  3. Not a Number. The text "3" is a String, so the raw add throws even though it looks numeric.

Frequently Asked Questions

It returns a wrapper that verifies, at runtime, that every inserted element is an instance of the given class, throwing ClassCastException otherwise.
Through raw types, unchecked casts, or reflection, all of which bypass compile-time generic checks.
The type check lets null through; whether it is stored depends on the backing collection.
In current JDKs, no. Every element is checked before any is added.
Each insert costs an extra isInstance check, which is cheap. Many developers use it mainly while debugging.

Next: checkedList()

Wrap a List so add, set, and list-iterator writes reject wrong types.

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

10 people found this page helpful