感谢作者能翻译并且分享这么优质的文章。不过文章中有一处表述欠妥:
如果希望避免这两个坑,请改用这个方式
Collections.addAll(arraylist, array);
从字面上理解,作者的意思应该是指避免上述的两个坑,定长
&& 修改原数据会影响list里的对象
。
欠妥的主要是后者:修改原数据,list中的值也会变
环境:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
Collections.addAll方法的源码如下所示:
@SafeVarargs
public static boolean addAll(Collection super T> c, T... elements) {
boolean result = false;
for (T element : elements)
result |= c.add(element);
return result;
}
Arrays.asList方法相关源码如下所示:
@SafeVarargs
@SuppressWarnings("varargs")
public static List asList(T... a) {
return new ArrayList<>(a);// 内部私有类
}
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
public static T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
从源码来看,不管是Collections.addAll,还是Arrays.asList,均是引用传递,修改source object会影响到list里的target object。下面是我测试的代码:
public static void main(String[] args) {
User user_1 = new User();
user_1.put("name", "user_1");
user_1.put("value", "v_1");
User user_2 = new User();
user_2.put("name", "user_2");
user_2.put("value", "v_2");
List asList = Arrays.asList(user_1, user_2);
List arraylist = new ArrayList<>();
Collections.addAll(arraylist, user_1, user_2);
user_1.put("value", "v_a_1");
user_2.put("value", "v_a_2");
System.out.println("-------- asList --------");
System.out.println(JsonKit.toJson(asList));
System.out.println("\n-------- arraylist --------");
System.out.println(JsonKit.toJson(arraylist));
}
运行结果
-------- asList --------
[{"name":"user_1","value":"v_a_1"},{"name":"user_2","value":"v_a_2"}]
-------- arraylist --------
[{"name":"user_1","value":"v_a_1"},{"name":"user_2","value":"v_a_2"}]
结论,不管是Collections.addAll还是Arrays.asList,修改原始数据时,均会影响到List里的数据
原帖地址