[Java]List 복사하는 방법
List를 복사하는 4가지 방법
- 생성자를 이용하는 방법
- ArrayList.clone()를 이용하는 방법
- List.addAll() 이용하는 방법
- Stream 을 이용하는 방법
위 4가지 모두 요소의 참조주소를 복사하는 얕은복사(Shallow Copy) 방법이다.
객체의 참조값도 모두 새로 생성하는 깊은복사(Deep Copy)를 하고 싶은 경우 개발자가 직접 구현해야한다.(반복문 사용 등)
예제코드는 아래를 참조하자.
public class ListCopyTest { @Data class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } } @Test @DisplayName("ArrayList 생성자를 사용하여 배열을 복사할 수 있다.(얕은복사)") void copyListTest1() { // given List<Person> original = new ArrayList<>(); original.add(new Person(10, "person1")); original.add(new Person(20, "person2")); original.add(new Person(30, "person3")); // when List<Person> copiedList = new ArrayList<>(original); copiedList.get(0).setAge(40); original.get(1).setAge(50); // then Assertions.assertThat(original == copiedList).isFalse(); Assertions.assertThat(original.equals(copiedList)).isTrue(); System.out.println("original = " + original); System.out.println("copiedList = " + copiedList); } @Test @DisplayName("ArrayList.clone()를 사용하여 배열을 복사할 수 있다.(얕은복사)") void copyListTest2() { // given ArrayList<Person> original = new ArrayList<>(); original.add(new Person(10, "person1")); original.add(new Person(20, "person2")); original.add(new Person(30, "person3")); // when ArrayList<Person> copiedList = (ArrayList<Person>) original.clone(); copiedList.get(0).setAge(40); original.get(1).setAge(50); // then Assertions.assertThat(original == copiedList).isFalse(); Assertions.assertThat(original.equals(copiedList)).isTrue(); System.out.println("original = " + original); System.out.println("copiedList = " + copiedList); } @Test @DisplayName("List.addAll을 사용하여 배열을 복사할 수 있다.(얕은복사)") void copyListTest3() { // given List<Person> original = new ArrayList<>(); original.add(new Person(10, "person1")); original.add(new Person(20, "person2")); original.add(new Person(30, "person3")); List<Person> copiedList = new ArrayList<>(); // when copiedList.addAll(original); copiedList.get(0).setAge(40); original.get(1).setAge(50); // then Assertions.assertThat(original == copiedList).isFalse(); Assertions.assertThat(original.equals(copiedList)).isTrue(); System.out.println("original = " + original); System.out.println("copiedList = " + copiedList); } @Test @DisplayName("Stream 을 사용하여 배열을 복사할 수 있다.(얕은복사)") void copyListTest4() { // given ArrayList<Person> original = new ArrayList<>(); original.add(new Person(10, "person1")); original.add(new Person(20, "person2")); original.add(new Person(30, "person3")); // when List<Person> copiedList = original.stream() .collect(Collectors.toList()); copiedList.get(0).setAge(40); original.get(1).setAge(50); // then Assertions.assertThat(original == copiedList).isFalse(); Assertions.assertThat(original.equals(copiedList)).isTrue(); System.out.println("original = " + original); System.out.println("copiedList = " + copiedList); } @Test @DisplayName("복사된 리스트에 요소를 추가해도, 원본 리스트에 영향을 주지 않는다.") void copyListTest5() { // given ArrayList<Person> original = new ArrayList<>(); original.add(new Person(10, "person1")); original.add(new Person(20, "person2")); original.add(new Person(30, "person3")); // when List<Person> copiedList = new ArrayList<>(original); copiedList.add(new Person(40, "person4")); // then Assertions.assertThat(original == copiedList).isFalse(); Assertions.assertThat(original.equals(copiedList)).isFalse(); Assertions.assertThat(original.size()).isNotEqualTo(copiedList.size()); System.out.println("original = " + original); System.out.println("copiedList = " + copiedList); } }
최신 댓글