Collections public static List checkedList(List list, Class type) Method Example Program


Returns a dynamically typesafe view of the specified list.

Program

package com.candidjava;

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

/**
 * 
 * @author karthikeyan.T
 * @description Returns a dynamically typesafe view of the specified list.
 */
public class CollectionsCheckedList {

	public static void main(String args[]) {
		List<String> arlst = new ArrayList<String>();
		arlst.add("Karthik");
		arlst.add("Mohan");
		arlst.add("Kamal");
		arlst.add("Hari");
		arlst.add("Anand");
		arlst.add("Vinod");
		List<String> tslst = Collections.checkedList(arlst, String.class);
		System.out.println(tslst);
	}
}

Output

[Karthik, Mohan, Kamal, Hari, Anand, Vinod]

Explanation

public static <E> List<E> checkedList(List<E> list, Class<E> type)
Returns a dynamically typesafe view of the specified list. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a list contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all subsequent access to the list takes place through the view, it is guaranteed that the list cannot contain an incorrectly typed element.
A discussion of the use of dynamically typesafe views may be found in the documentation for the checkedCollection method.

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.

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
Returns:
a dynamically typesafe view of the specified list
Since:
1.5


Related Post

Comments


©candidjava.com