Java Collections checkedList() Method

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

Definition

In Java, Collections.checkedList(list, type) returns a List wrapper that checks the type of every element written into it, through add, add(index, e), set, addAll, or a ListIterator.

Compile-time generics stop most mistakes, but code using a raw List can still insert anything. The checked wrapper turns that silent corruption into an immediate ClassCastException.

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

Syntax

Pass the list and its element class.

Java
List<String> names = Collections.checkedList(new ArrayList<String>(), String.class);
List raw = names;
raw.set(0, 99);    // ClassCastException
CallWhat it does
raw.add(x) / add(i, x)Checked.
raw.set(i, x)Checked.
listIterator().set(x) / add(x)Checked.
subList(a, b)Returns a checked list too.

Added in Java 5. Reading, removing, and sorting work exactly as on the original list; only writes that introduce a value are checked.

Library declarations
Java
// java.util.Collections
public static <E> List<E> checkedList(List<E> list, Class<E> type)

Parameters

ParameterDescription
listThe list to protect.
typeThe class every inserted element must belong to.

Return Value

Return ValueDescription
List<E>A dynamically type-safe view of list.

Compare with Similar Methods

Related wrappers:

MethodHow it differs
checkedCollection(c, type)For any collection.
synchronizedList(l)Thread-safe list; can wrap a checked list.
unmodifiableList(l)No writes at all.

Java Examples

Example 1 — Positional Writes Are Checked

A raw reference tries to replace and insert numbers into a list of names.

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

public class CollectionsCheckedListSet {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        List<String> names = Collections.checkedList(new ArrayList<>(Arrays.asList("ana", "ben")), String.class);
        List raw = names;

        try {
            raw.set(0, 99);
        } catch (ClassCastException e) {
            System.out.println("set(0, 99): " + e.getClass().getSimpleName());
        }
        try {
            raw.add(1, 3.5);
        } catch (ClassCastException e) {
            System.out.println("add(1, 3.5): " + e.getClass().getSimpleName());
        }
        raw.set(0, "amy");
        System.out.println(names);
    }
}

How It Works

  1. Raw alias. raw is the same list without generics, so the compiler allows any argument.
  2. Both rejected. An Integer through set and a Double through add(index, e) each fail the runtime check.
  3. Correct type passes. Setting "amy" at index 0 works, even through the raw reference.

Example 2 — ListIterator Writes Are Checked Too

Edit a list of scores while iterating: a wrong-type set fails, a correct add succeeds.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class CollectionsCheckedListIterator {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        List<Integer> scores = Collections.checkedList(new ArrayList<>(Arrays.asList(10, 20)), Integer.class);
        ListIterator raw = scores.listIterator();
        raw.next();

        try {
            raw.set("ten");
        } catch (ClassCastException e) {
            System.out.println("iterator set: " + e.getClass().getSimpleName());
        }
        raw.add(15);
        System.out.println(scores);
    }
}

How It Works

  1. Wrapped iterator. The checked list hands out a ListIterator that checks its own set and add.
  2. Rejected replace. After next() returns 10, replacing it with the text "ten" throws, and 10 stays.
  3. Accepted insert. add(15) inserts at the cursor, right after 10, giving [10, 15, 20].

Example 3 — A subList of a Checked List Is Checked

Write through a slice of the list with a raw reference, then with the right type.

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

public class CollectionsCheckedListSubList {
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static void main(String[] args) {
        List<String> words = Collections.checkedList(new ArrayList<>(Arrays.asList("a", "b", "c", "d")), String.class);
        List<String> middle = words.subList(1, 3);

        List raw = middle;
        try {
            raw.add(Boolean.TRUE);
        } catch (ClassCastException e) {
            System.out.println("subList add: " + e.getClass().getSimpleName());
        }
        middle.add("x");
        System.out.println("middle " + middle + ", words " + words);
    }
}

How It Works

  1. Slice is wrapped. subList(1, 3) on a checked list returns another checked list over b and c.
  2. No loophole. Adding a Boolean through the slice fails just like adding to the full list.
  3. Writes go through. "x" is appended to the end of the slice, which places it before "d" in the full list.

Frequently Asked Questions

It returns a List view that verifies the type of every element added or set, throwing ClassCastException for wrong types.
add, add(index, e), set, addAll, and add or set through a ListIterator. subList views are checked too.
When raw types, unchecked casts, or older libraries might put the wrong type into your list and you want to catch it at the source.
Yes, for example Collections.synchronizedList(Collections.checkedList(list, String.class)).
Java 5.

Next: checkedMap()

Wrap a Map so wrong key or value types are rejected on insert.

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

11 people found this page helpful