Collections.checkedList(List, Class) in Java

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

Syntax:

<E> List<E> java.util.Collections.checkedList(List<E> list, Class<E> type)

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

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.

list: the list for which a dynamically typesafe view is to be returned.

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

Returns: a dynamically typesafe view of the specified list.

Exceptions: NA

Approach

Java

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

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

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

        arrlist.add(12);

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

    }
}

Output:

[12]


No comments:

Post a Comment