The checkedList() method of Collections class is present inside java.util package is used to return a dynamically typesafe view of the specified list. The key thing to note here is that the returned list will be serializable if the specified list is serializable. Since null is considered to be a value of any reference type, the returned list permits the insertion of null elements whenever the backing list does.
Tip: This method is compatible with java version 1.5 and onwards.
Syntax:Â
public static List checkedList(List list, Class 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: A dynamically typesafe view of the specified list.
Exceptions: This method throws ClassCastExceptionÂ
Example 1:
Java
// Java program to Demonstrate checkedList() method// of Collections class for a string valueÂ
// Importing required classesimport java.util.*;Â
// Main classpublic 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);        }    }} |
List: [A, B, C, TajMahal] Typesafe view of List: [A, B, C, TajMahal]
Â
Example 2:
Java
// Java program to Demonstrate checkedList() method// of Collections class for a string valueÂ
// Importing required classesimport java.util.*;Â
// Main classpublic 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);        }    }} |
List: [20, 30, 40, 50] Typesafe view of List: [20, 30, 40, 50]
Â
