Collections.emptyList(), Collections.emptyMap() 分别表示一个空的list 和 map.
只是表示它们不是null. 但要在里面添加元素会报错.
从里面取元素也会报异常!
刷新我们原来的预想了!
大坑不能不防!
public static void main(String[] args) {
List list=Collections.emptyList();
System.out.println(list==null);
// System.out.println(list.size()); //空集合,用了list.get(0)也会报异常
// System.out.println(list.get(0)); //Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0
// list.add("abc"); //不能放元素到里面. Exception in thread "main" java.lang.UnsupportedOperationException
Map map=Collections.emptyMap();
System.out.println(map.size());
// map.put("abc", "test"); //不能添加元素到里面. java.lang.UnsupportedOperationException
System.out.println(map.get("a")); //这个不会.
}