方式一:
List<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");
List<String> l2 = new ArrayList<String>(l);
l2.add("d");
System.out.println(l2);
System.out.println(l);
跑的結果:
[a, b, c, d]
[a, b, c]
說明:通過new ArrayList<T>(List<T> srcList)的方式複制一個集合,是單獨開辟了一個記憶體空間,并且内容跟源集合的内容一樣。對新集合的修改并不影響源集合。
方式二:
List<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");
List<String> l2 = new ArrayList<String>();
l2.addAll(l);
l2.add("d");
System.out.println(l2);
System.out.println(l);
列印結果:
[a, b, c, d]
[a, b, c]
說明:通過addAll的方法複制一個集合,新的集合也是單獨開辟了一個記憶體空間。當然新集合的操作不影響源集合。
方式三:
List<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");
List<Object> l2 = new ArrayList<Object>(Arrays.asList(new Object[l.size()]));
Collections.copy(l2, l);
l2.add("d");
System.out.println(l2);
System.out.println(l);
列印結果:
[a, b, c, d]
[a, b, c]
說明:使用集合的工具類的靜态方法Collections.copy(List desList, List srcList)的方式複制集合,得到的也是一個位于記憶體中不同空間的副本。
注意這種方式,如果你直接這樣寫:
List<String> l2 = new ArrayList<String>();
Collections.copy(l2, l);
會報錯:java.lang.IndexOutOfBoundsException: Source does not fit in dest
原因是你使用該方法時,會先比較目标集合和源集合的size,而你直接new ArrayList<String>();還沒來得及複制,目标集合的size為0,和源集合的size不一樣,就會報錯。注:new ArrayList(int size)定義的是該集合的的容納能力為size,并不是說目标集合中就有了size個元素。是以要這樣寫:new ArrayList<Object>(Arrays.asList(new Object[l.size()]));
方式四:
List<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");
List<String> l2 = l;
l2.add("d");
System.out.println(l2);
System.out.println(l);
這次的列印結果為:
[a, b, c, d]
[a, b, c, d]
說明:這種方式隻是定義了另一個引用,但是指向的内容還是源集合指向的内容。是以對其修改當然會影響源集合了。
方式五:流式拷貝(适用于集合類型為非一般類型的情形)
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}
對Map, Set的複制拷貝方式研究同理,這裡不再贅述。