Collections checkedCollection() method in Java with Examples

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 the following arguments as a parameter:

  • c – the collection for which a dynamically typesafe view is to be returned
  • type – the type of element that c is permitted to hold

Return Value: This method returns a dynamically typesafe view of the specified collection

Below are the examples to illustrate the checkedCollection() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checkedCollection() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
  
            // creating object of List<String>
            List<String> arlst = new ArrayList<String>();
  
            // Adding element to arrlist
            arlst.add("CSS");
            arlst.add("PHP");
            arlst.add("HTML");
            arlst.add("TajMahal");
  
            // printing the arrlist
            System.out.println("List: " + arlst);
  
            // create typesafe view of the collection
            Collection<String>
                tslst = Collections
                            .checkedCollection(arlst, String.class);
  
            // printing the arrlist after operation
            System.out.println("Typesafe view of List: " + tslst);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

List: [CSS, PHP, HTML, TajMahal]
Typesafe view of List: [CSS, PHP, HTML, TajMahal]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checkedCollection() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of List<Integer>
            List<Integer> arlst = new ArrayList<Integer>();
  
            // Adding element to arrlist
            arlst.add(20);
            arlst.add(30);
            arlst.add(40);
            arlst.add(50);
  
            // printing the arrlist
            System.out.println("List: " + arlst);
  
            // create typesafe view of the collection
            Collection<Integer>
                tslst = Collections
                            .checkedCollection(arlst, Integer.class);
  
            // printing the arrlist after operation
            System.out.println("Typesafe view of List: " + tslst);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

List: [20, 30, 40, 50]
Typesafe view of List: [20, 30, 40, 50]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai