Collections.singletonList,返回只有一个元素的集合,省内存,因为默认的List在JDK1.8之后初始化后add元素之后的size=10
其他特殊的容器类
下面6个Collections
中的方法返回的容器类均是immutable
,即只读的,如果调用修改接口(比如说add
,set
,put
等),将会抛出UnsupportedOperationException
public static <T> Set<T> singleton(T o);
public static <T> List<T> singletonList(T o);
public static <K,V> Map<K,V> singletonMap(K key, V value);
// 或者直接调用常量 EMPTY_LIST
public static final <T> List<T> emptyList();
//或者直接调用常量 EMPTY_MAP
public static final <K,V> Map<K,V> emptyMap();
//或者直接调用常量 EMPTY_SET
public static final <T> Set<T> emptySet()