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