The Collections.checkedList() method in Java returns a runtime type-safe view of the specified list. It ensures that only elements of the specified type can be added to the list. If an element of an incorrect type is inserted, a ClassCastException is thrown. This method is useful when working with legacy code or raw collections where compile-time type checking is not sufficient.
- Allows only the specified element type.
- Supports null values if the backing list allows them.
import java.util.*;
public class CheckedListDemo {
public static void main(String[] args) {
// Creating a list
List<String> languages = new ArrayList<>();
// Creating a type-safe view of the list
List<String> checkedList =
Collections.checkedList(languages, String.class);
// Adding elements
checkedList.add("Java");
checkedList.add("Python");
checkedList.add("C++");
// Displaying the list
System.out.println("Checked List: " + checkedList);
}
}
Output
Checked List: [Java, Python, C++]
Explanation: This example creates a type-safe view of an ArrayList using Collections.checkedList(). The returned list allows only String elements and helps prevent insertion of objects of the wrong type at runtime.
Syntax
public static <E> List<E> checkedList(List<E> list, Class<E> type)
Parameters: This method takes the following arguments as parameters:
- The list for which a dynamically typesafe view is to be returned
- The type of element that list is permitted to hold
- Return Type: Returns a runtime type-safe view of the specified list.
- Exceptions: ClassCastException –> Thrown if an element of the wrong type is added to the checked list.
Example: Program to Demonstrate checkedList() method of Collections class for a string value
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating an ArrayList of string type by
// declaring object of List
List<String> arlst = new ArrayList<String>();
// Adding element to ArrayList
// by using standard add() method
// Custom input elements
arlst.add("A");
arlst.add("B");
arlst.add("C");
arlst.add("TajMahal");
// Printing the above elements inside ArrayList
System.out.println("List: " + arlst);
// Creating typesafe view of the specified list
// and applying checkedList
List<String> tslst = Collections.checkedList(
arlst, String.class);
// Printing the updated elements of ArrayList
// after applying above operation
System.out.println("Typesafe view of List: "
+ tslst);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message on console if exception
// occurs
System.out.println("Exception thrown : " + e);
}
}
}
Output
List: [A, B, C, TajMahal] Typesafe view of List: [A, B, C, TajMahal]
Explanation: This example creates a type-safe view of a String list using Collections.checkedList(). The returned list allows only String elements and displays the same contents as the original list.
Example: Creating a Type-Safe Integer List Using checkedList()
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating an empty ArrayList of integer type
// by creating an object of List
List<Integer> arlst = new ArrayList<Integer>();
// Adding element to above ArrayList
// by using add() method
arlst.add(20);
arlst.add(30);
arlst.add(40);
arlst.add(50);
// Printing the elements of above ArrayList
System.out.println("List: " + arlst);
// Creating typesafe view of the specified list
// with usage of checkedList() method
List<Integer> tslst = Collections.checkedList(
arlst, Integer.class);
// Printing the elements of ArrayList
// after performing above operation
System.out.println("Typesafe view of List: "
+ tslst);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message if exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
Output
List: [20, 30, 40, 50] Typesafe view of List: [20, 30, 40, 50]
Explanation: This example creates a type-safe view of an Integer list using Collections.checkedList(). It ensures that only Integer values can be added, providing runtime type safety.
Advantages of checkedList()
- Provides runtime type safety.
- Prevents adding elements of the wrong type.
- Helps detect type-related errors early.
- Useful when working with legacy or raw collections.
- Improves code reliability and maintainability.