java.util.Collections.checkedSet() 方法
描述
checkedSet(Set<E>, Class<E>) 方法用于获取指定集合的动态类型安全视图。
声明
以下是 java.util.Collections.checkedSet() 方法的声明。
public static <E> Set<E> checkedSet(Set<E> s, Class<E> type)
参数
s − 这是要为其返回动态类型安全视图的集合。
type − 这是允许 s 持有的元素的类型。
返回值
方法调用返回指定集合的动态类型安全视图。
异常
NA
示例
下面的例子展示了 java.util.Collections.checkedSet() 的用法。
package com.tutorialspoint;
import java.util.*;
public class CollectionsDemo {
public static void main(String args[]) {
// create set
TreeSet<String> hset = new TreeSet<String>();
// populate the set
hset.add("Collections");
hset.add("in");
hset.add("java");
// get typesafe view of the set
Set<String> tsset = Collections.checkedSet(hset,String.class);
System.out.println("Dynamically typesafe view of the set: "+tsset);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Dynamically typesafe view of the set: [Collections, in, java]

