Open In App

Collections checkedList() method in Java with Examples

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 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]

 

Example 2:

Java




// Java 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 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]

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads