Home »
Java programming language
Java Collections checkedList() Method with Example
Collections Class checkedList() method: Here, we are going to learn about the checkedList() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on January 07, 2020
Collections Class checkedList() method
- checkedList() Method is available in java.lang package.
- checkedList() Method is used to return the typesafe view of the given List at runtime.
- checkedList() Method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- checkedList() Method does not throw an exception at the time of returning validated List.
Syntax:
public static List checkedList(List l, Class ele_ty);
Parameter(s):
- List l – represents the list for which to get typesafe view at runtime.
- Class ele_ty – represents the element type (ele_ty) that the given List is allowed to store.
Return value:
The return type of the method is List, it returns typesafe or validated view of the given List dynamically.
Example:
// Java Program is to demonstrate the example
// of List checkedList(List l, Class ele_ty) of Collections class
import java.util.*;
public class CheckedList {
public static void main(String args[]) {
// Create a linked list object
LinkedList < Integer > link_list = new LinkedList < Integer > ();
// By using add() method is to add the
// given elements in linked list
link_list.add(20);
link_list.add(10);
link_list.add(30);
link_list.add(40);
link_list.add(50);
// Display LinkedList
System.out.println("link_list: " + link_list);
// By using checkedList() method is to
// represent the type safe view of the given
// Collection linked list
Collection < Integer > co = Collections.checkedList(link_list, Integer.class);
System.out.println();
System.out.println("Collections.checkedList(link_list, Integer.class) :");
// Display collection
System.out.println("co: " + co);
}
}
Output
link_list: [20, 10, 30, 40, 50]
Collections.checkedList(link_list, Integer.class) :
co: [20, 10, 30, 40, 50]