Collections.checkedCollection(Collection,Class) in Java

Collections.checkedCollection(Collection,Class): This method is available in java.util.Collections class of Java.

Syntax:

<E> Collection<E> java.util.Collections.checkedCollection(Collection<E> c, Class<E> type)

This method takes two arguments. This method returns a dynamical type safe view of the specified collection.

Note: Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.

Parameters: Two parameters are required for this method.

c: the collection for which a dynamical type the safeview is to be returned.

type: the type of element that c is permitted to hold.

Returns: a dynamical type safe view of the specified collection.

Exceptions: NA

Approach

Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionscheckedCollection {
    public static void main(String[] args) {

        List<Integer> arrlist = new ArrayList<Integer>();

        arrlist.add(12);
        arrlist.add(2);

        arrlist.add(14);
        arrlist.add(6);

      System.out.println(Collections.checkedCollection(arrlist,
Integer.class));

    }
}

Output:

[12, 2, 14, 6]


No comments:

Post a Comment