emptySet() 方法用于获取空集(不可变)。这集是可序列化的。
声明
以下是java.util.Collections.emptySet()方法的声明。
public static final <T> Set<T> emptySet()
参数
-
NA
返回值
NA
异常
-
NA
例子
下面的例子显示java.util.Collections.emptySet()方法的使用
package com.yiibai; import java.util.*; public class CollectionsDemo { public static void main(String args[]) { // create an empty set Set emptyset = Collections.emptySet(); System.out.println("Created empty immutable set: "+emptyset); // try to add elements emptyset.add("Adding"); } }
让我们来编译和运行上面的程序,这将产生以下结果。该组是不可变的,这么添加元素将抛出异常。
Created empty immutable set: [] Exception in thread "main" java.lang.UnsupportedOperationException