The checkedCollection() method of java.util.Collections class is used to return a dynamically typesafe view of the specified collection. The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object’s equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list. The returned collection will be serializable if the specified collection is serializable.
Since null is considered to be a value of any reference type, the returned collection permits insertion of null elements whenever the backing collection does.
Syntax:Â
public static Collection checkedCollection(Collection c, Class type)
Parameters: This method takes two parametersÂ
- The collection for which a dynamically typesafe view is to be returned
- The type of element that c is permitted to hold
Return Type: This method returns a dynamically typesafe view of the specified collection
Example 1:
Java
// Java program to demonstrate// checkedCollection() method// for String valueÂ
// Importing utility 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 by            // creating object of List of string type            List<String> arlst = new ArrayList<String>();Â
            // Adding element to arrlist            // using add() methodÂ
            // Custom input elements            arlst.add("CSS");            arlst.add("PHP");            arlst.add("HTML");            arlst.add("TajMahal");Â
            // Printing the ArrayList            System.out.println("List: " + arlst);Â
            // Now create typesafe view of the collection by            // creating object of Collection class of string            // type            Collection<String> tslst                = Collections.checkedCollection(                    arlst, String.class);Â
            // Printing the updated ArrayList            // after applying checkedCollection() method            System.out.println("Typesafe view of List: "                               + tslst);        }Â
        // Catch block to handle the exceptions        catch (IllegalArgumentException e) {Â
            // Print message to be displayed on console            // when exception occurs            System.out.println("Exception thrown : " + e);        }    }} |
List: [CSS, PHP, HTML, TajMahal] Typesafe view of List: [CSS, PHP, HTML, TajMahal]
Â
Example 2:
Java
// Java program to Illustrate checkedCollection() method// of Collection class for an Integer valueÂ
// Importing classesimport java.util.*;Â
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] argv) throws Exception    {Â
        // Try block to check for exception        try {Â
            // Creating an empty ArrayList of integer type            // by declaring object of List            List<Integer> arlst = new ArrayList<Integer>();Â
            // Adding element to ArrayList            // using add() method            arlst.add(20);            arlst.add(30);            arlst.add(40);            arlst.add(50);Â
            // Printing the above ArrayList            System.out.println("List: " + arlst);Â
            // now creating typesafe view of the collection            // using checkedCollection() method            Collection<Integer> tslst                = Collections.checkedCollection(                    arlst, Integer.class);Â
            // Printing the updated ArrayList            // after above operation            System.out.println("Typesafe view of List: "                               + tslst);        }Â
        // Catch block to handle 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]
Â
