java.util.Collections.checkedCollection() Method



Description

The checkedCollection(Collection<E>, Class<E>) method is used to get a dynamically typesafe view of the specified collection.

Declaration

Following is the declaration for java.util.Collections.checkedCollection() method.

public static <E> Collection<E> checkedCollection(Collection<E> c,Class<E> type)

Parameters

  • c − This is the collection for which a dynamically typesafe view is to be returned.

  • type − This is the type of element that c is permitted to hold.

Return Value

The method call returns a dynamically typesafe view of the specified collection.

Exception

NA

Example

The following example shows the usage of java.util.Collections.checkedCollection()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create arraylist       
      ArrayList<String> arlst = new ArrayList<String>();

      // populate the list
      arlst.add("TP");
      arlst.add("PROVIDES");
      arlst.add("QUALITY");
      arlst.add("TUTORIALS");

      // create typesafe view of the collection
      Collection<String> tslst;
      tslst = Collections.checkedCollection(arlst,String.class);     

      System.out.println("Type safe view is: "+tslst);
   }    
}

Let us compile and run the above program, this will produce the following result.

Type safe view is: [TP, PROVIDES, QUALITY, TUTORIALS]
java_util_collections.htm
Advertisements