Home Java Collections Methods checkedList() Java Collections checkedList() Method 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.
List<String> names = Collections.checkedList(new ArrayList<String>(), String.class);
List raw = names;
raw.set(0, 99); // ClassCastException Call What 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.util.Collections
public static <E> List<E> checkedList(List<E> list, Class<E> type) Parameters Parameter Description listThe list to protect. typeThe class every inserted element must belong to.
Return Value Return Value Description List<E>A dynamically type-safe view of list.
Compare with Similar Methods Related wrappers:
Java Examples 1. set and add(index) 2. ListIterator 3. subList Example 1 — Positional Writes Are Checked A raw reference tries to replace and insert numbers into a list of names.
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);
}
} set(0, 99): ClassCastException
add(1, 3.5): ClassCastException
[amy, ben] How It Works Raw alias. raw is the same list without generics, so the compiler allows any argument.Both rejected. An Integer through set and a Double through add(index, e) each fail the runtime check.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.
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);
}
} iterator set: ClassCastException
[10, 15, 20] How It Works Wrapped iterator. The checked list hands out a ListIterator that checks its own set and add.Rejected replace. After next() returns 10, replacing it with the text "ten" throws, and 10 stays.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.
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);
}
} subList add: ClassCastException
middle [b, c, x], words [a, b, c, x, d] How It Works Slice is wrapped. subList(1, 3) on a checked list returns another checked list over b and c.No loophole. Adding a Boolean through the slice fails just like adding to the full list.Writes go through. "x" is appended to the end of the slice, which places it before "d" in the full list.Frequently Asked Questions What does Collections.checkedList() do? It returns a List view that verifies the type of every element added or set, throwing ClassCastException for wrong types.
Which operations are checked? add, add(index, e), set, addAll, and add or set through a ListIterator. subList views are checked too.
When do I need checkedList()? 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.
Can I combine checkedList() with synchronizedList()? Yes, for example Collections.synchronizedList(Collections.checkedList(list, String.class)).
Which Java version added checkedList()? Java 5.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion