Java Collections CheckSet()方法与示例

集合类的checkedSet()方法 (Collections Class checkedSet() method)

  • checkedSet() Method is available in java.lang package.

    CheckedSet()方法在java.lang包中可用。

  • checkedSet() Method is used to returns the typesafe view of the given set at runtime.

    checkedSet()方法用于在运行时返回给定集合的类型安全视图。

  • checkedSet() 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.

    checkedSet()方法是一个静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。

  • checkedSet() Method does not throw an exception at the time of returning validated set.

    返回验证的集合时, checkedSet()方法不会引发异常。

Syntax:

句法:

    public static Set checkedSet(Set set, Class ele_ty);

Parameter(s):

参数:

  • Set set – represents the set for which to get typesafe view of the given Set(set).

    Set set –表示要获取给定Set(set)的类型安全视图的集合。

  • Class ele_ty – represents the type of elements (ele_ty) that the given set is allowed to store.

    ele_ty类 –表示允许给定集合存储的元素类型(ele_ty)。

Return value:

返回值:

The return type of the method is Set, it returns typesafe view of the given set dynamically.

方法的返回类型为Set ,它动态返回给定集合的类型安全视图。

Example:

例:

// Java Program is to demonstrate the example
// of Set checkedSet(Set set, Class ele_ty) of Collections class

import java.util.*;

public class CheckedSet {
    public static void main(String args[]) {
        // Create a hashset object    
        HashSet < Integer > hs = new HashSet < Integer > ();

        // By using ad() method is to add the
        // given elements in hash set
        hs.add(20);
        hs.add(10);
        hs.add(30);
        hs.add(40);
        hs.add(50);

        // Display HashSet
        System.out.println("hashset: " + hs);

        // By using checkedSet() method is to 
        // represent the type safe view of the given
        // Collection hashset

        Set < Integer > set = Collections.checkedSet(hs, Integer.class);

        System.out.println();
        System.out.println("Collections.checkedSet(hs, Integer.class) :");

        // Display collection
        System.out.println("set : " + set);
    }
}

Output

输出量

hashset: [50, 20, 40, 10, 30]

Collections.checkedSet(hs, Integer.class) :
set : [50, 20, 40, 10, 30]


翻译自: https://www.includehelp.com/java/collections-checkedset-method-with-example.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值