集合类的checkedCollection()方法 (Collections Class checkedCollection() method)
checkedCollection() Method is available in java.lang package.
DrawnCollection()方法在java.lang包中可用。
checkedCollection() Method is used to return the typesafe view of the given collection at runtime.
checkedCollection()方法用于在运行时返回给定集合的类型安全视图。
checkedCollection() 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.
checkedCollection()方法是一个静态方法,因此可以使用类名进行访问,如果我们尝试使用类对象访问该方法,则不会收到错误。
checkedCollection() Method does not throw an exception at the time of returning Collection.
返回Collection时, checkedCollection()方法不会引发异常。
Syntax:
句法:
public static Collection checkedCollection(Collection co, Class ele_ty);
Parameter(s):
参数:
Collection co – represent the collection for which to get a typesafe view at runtime.
集合co –表示在运行时为其获取类型安全视图的集合。
Class ele_ty – represent the element type that given collection is allowed to store.
ele_ty类 –表示允许给定集合存储的元素类型。
Return value:
返回值:
The return type of the method is Collection, it returns typesafe view of the given collection dynamically.
方法的返回类型为Collection ,它动态返回给定集合的typesafe视图。
Example:
例:
// Java Program is to demonstrate the example
// of Collection checkedCollection(Collection co, Class ele_ty) of
// Collections class
import java.util.*;
public class CheckedCollection {
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 checkedCollection() method is to
// represent the type safe view of the given Collection
Collection < Integer > co = Collections.checkedCollection(link_list, Integer.class);
System.out.println();
System.out.println("Collections.checkedCollection(link_list, Integer.class) :");
// Display collection
System.out.println("co: " + co);
}
}
Output
输出量
link_list: [20, 10, 30, 40, 50]Collections.checkedCollection(link_list, Integer.class) :
co: [20, 10, 30, 40, 50]
翻译自: https://www.includehelp.com/java/collections-checkedcollection-method-with-example.aspx