Java Collections CheckedCollection()方法与示例

集合类的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值