缘由:
private static List<Integer> getEmptyList() {
return Collections.emptyList();
}
public static void main(String[] args) {
try {
List<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5));
List<Integer> list2 = getEmptyList();
list2.addAll(list1);
System.out.println(list1.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
此方法会抛出以下异常:
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
...
原因在于
ArrayList里面有addAll的实现, 详见源码
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
而emptyList里面没有, 详见源码
/**
* @serial include
*/
private static class EmptyList<E>
extends AbstractList<E>
implements RandomAccess, Serializable {
private static final long serialVersionUID = 8842843931221139166L;
public Iterator<E> iterator() {
return emptyIterator();
}
public ListIterator<E> listIterator() {
return emptyListIterator();
}
public int size() {return 0;}
public boolean isEmpty() {return true;}
public boolean contains(Object obj) {return false;}
public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
public Object[] toArray() { return new Object[0]; }
public <T> T[] toArray(T[] a) {
if (a.length > 0)
a[0] = null;
return a;
}
public E get(int index) {
throw new IndexOutOfBoundsException("Index: "+index);
}
public boolean equals(Object o) {
return (o instanceof List) && ((List<?>)o).isEmpty();
}
public int hashCode() { return 1; }
// Preserves singleton property
private Object readResolve() {
return EMPTY_LIST;
}
}
但是, 如果我们这么写就可以避免这个异常
private static List<Integer> getEmptyList() {
return Collections.emptyList();
}
public static void main(String[] args) {
try {
List<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5));
List<Integer> list2 = getEmptyList();
list1.addAll(list2);
System.out.println(list1.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
程序可以输出
[1, 2, 3, 4, 5]
所以以后用 Collections.emptyList() 的时候要注意addAll的操作

探讨了使用Collections.emptyList()创建空列表时,addAll方法的行为及其实现原理,并提供了一个避免异常的示例。
1349

被折叠的 条评论
为什么被折叠?



