Wednesday, July 3, 2024
HomeLanguagesJavaCollections checkedList() method in Java with Examples

Collections checkedList() method in Java with Examples

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]

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments