java.util.Collections.emptyList() 方法
描述
emptyList() 方法用于获取空列表(不可变)。 这个列表是可序列化的。
声明
以下是 java.util.Collections.emptyList() 方法的声明。
public static final <T> List<T> emptyList()
参数
NA
返回值
NA
异常
NA
示例
下面的例子展示了 java.util.Collections.emptyList() 的用法。
package com.tutorialspoint;
import java.util.*;
public class CollectionsDemo {
public static void main(String args[]) {
// create an empty list
List<String> emptylst = Collections.emptyList();
System.out.println("Created empty immutable list: "+emptylst);
// try to add elements
emptylst.add("A");
emptylst.add("B");
}
}
让我们编译并运行上面的程序,这将产生以下结果。Exceptions 将被抛出,因为列表是不可变的。
Created empty immutable list: []
Exception in thread "main" java.lang.UnsupportedOperationException