Collections checkedList() method in Java with Examples
The checkedList() method of java.util.Collections class is used to return a dynamically typesafe view of the specified list.
The returned list will be serializable if the specified list is serializable.
Since null is considered to be a value of any reference type, the returned list permits insertion of null elements whenever the backing list does.
Syntax:
public static List
checkedList(List list, Class type)
Parameters: This method takes the following arguments as a parameters:
- list – the list for which a dynamically typesafe view is to be returned
- type – the type of element that list is permitted to hold
Return Value: This method returns a dynamically typesafe view of the specified list
Below are the examples to illustrate the checkedList() method
Example 1:
// Java program to demonstrate // checkedList() 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 arlst arlst.add("A"); arlst.add("B"); arlst.add("C"); arlst.add("TajMahal"); // printing the arrlist System.out.println("List: " + arlst); // create typesafe view of the specified list List<String> tslst = Collections .checkedList(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); } } } |
List: [A, B, C, TajMahal] Typesafe view of List: [A, B, C, TajMahal]
Example 2:
// Java program to demonstrate // checkedList() 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 arlst 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 specified list List<Integer> tslst = Collections .checkedList(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); } } } |
List: [20, 30, 40, 50] Typesafe view of List: [20, 30, 40, 50]
Recommended Posts:
- Collections min() method in Java with Examples
- Collections max() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections newSetFromMap() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections checkedMap() method in Java with Examples
- Collections list() method in Java with Examples
- Collections indexOfSubList() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections unmodifiableMap() method in Java with Examples
- Collections unmodifiableList() method in Java with Examples
- Collections unmodifiableSet() method in Java with Examples
- Collections unmodifiableSortedMap() method in Java with Examples
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



