java.util.Collections.unmodifiableSet() 方法
描述
unmodifiableSet() 方法返回指定集合的不可修改视图。
声明
以下是 java.util.Collections.unmodifiableSet() 方法的声明。
public static <T> boolean addAll(Collection<? super T> c, T.. a)
参数
s − 这是要为其返回不可修改视图的集合。
返回值
方法调用返回指定集合的不可修改视图。
异常
NA
示例
下面的例子展示了 java.util.Collections.unmodifiableSet() 的用法。
package com.tutorialspoint;
import java.util.*;
public class CollectionsDemo {
public static void main(String[] s) {
// create set
Set<String> set = new HashSet<String>();
// populate the set
set.add("Welcome");
set.add("to");
set.add("TP");
System.out.println("Initial set value: "+set);
// create unmodifiable set
Set unmodset = Collections.unmodifiableSet(set);
// try to modify the set
unmodset.add("Hello");
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Initial set value: [to, Welcome, TP] Exception in thread "main" java.lang.UnsupportedOperationException

