diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopyarraylist/Course.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopyarraylist/Course.java new file mode 100644 index 0000000000..5724be7218 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopyarraylist/Course.java @@ -0,0 +1,42 @@ +package com.baeldung.deepcopyarraylist; + +import java.io.Serializable; + +public class Course implements Serializable, Cloneable { + + private Integer courseId; + private String courseName; + + public Course() { + } + + public Course(Integer courseId, String courseName) { + this.courseId = courseId; + this.courseName = courseName; + } + + public Integer getCourseId() { + return courseId; + } + + public void setCourseId(Integer courseId) { + this.courseId = courseId; + } + + public String getCourseName() { + return courseName; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + @Override + public Course clone() { + try { + return (Course) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } +} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopyarraylist/Student.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopyarraylist/Student.java new file mode 100644 index 0000000000..fedc9010ab --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopyarraylist/Student.java @@ -0,0 +1,103 @@ +package com.baeldung.deepcopyarraylist; + +import java.io.Serializable; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.SerializationUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class Student implements Serializable, Cloneable { + + private int studentId; + private String studentName; + private Course course; + + public Student() { + } + + public Student(int studentId, String studentName, Course course) { + this.studentId = studentId; + this.studentName = studentName; + this.course = course; + } + + public Student(Student student) { + this.studentId = student.getStudentId(); + this.studentName = student.getStudentName(); + this.course = new Course(student.getCourse() + .getCourseId(), student.getCourse() + .getCourseName()); + } + + public static Student createDeepCopy(Student student) { + ObjectMapper objectMapper = new ObjectMapper(); + try { + return objectMapper.readValue(objectMapper.writeValueAsString(student), Student.class); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException(e); + } + } + + public static List deepCopyUsingJackson(List students) { + return students.stream() + .map(Student::createDeepCopy) + .collect(Collectors.toList()); + } + + public static List deepCopyUsingCloneable(List students) { + return students.stream() + .map(Student::clone) + .collect(Collectors.toList()); + } + + public static List deepCopyUsingCopyConstructor(List students) { + return students.stream() + .map(Student::new) + .collect(Collectors.toList()); + } + + public static List deepCopyUsingSerialization(List students) { + return students.stream() + .map(SerializationUtils::clone) + .collect(Collectors.toList()); + } + + public int getStudentId() { + return studentId; + } + + public void setStudentId(int studentId) { + this.studentId = studentId; + } + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + @Override + public Student clone() { + Student student; + try { + student = (Student) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + student.course = this.course.clone(); + return student; + } +} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopyarraylist/DeepCopyArrayListUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopyarraylist/DeepCopyArrayListUnitTest.java new file mode 100644 index 0000000000..625c7e8385 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopyarraylist/DeepCopyArrayListUnitTest.java @@ -0,0 +1,77 @@ +package com.baeldung.deepcopyarraylist; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +public class DeepCopyArrayListUnitTest { + + @Test + public void whenCreatingCopyWithCloneable_thenObjectsShouldNotBeSame() { + Course course = new Course(1, "Spring Masterclass"); + Student student1 = new Student(1, "John", course); + Student student2 = new Student(2, "David", course); + List students = new ArrayList<>(); + students.add(student1); + students.add(student2); + + List deepCopy = Student.deepCopyUsingCloneable(students); + + Assertions.assertNotEquals(students.get(0), deepCopy.get(0)); + Assertions.assertNotEquals(students.get(1), deepCopy.get(1)); + + } + + @Test + public void whenCreatingDeepCopyWithCopyConstructor_thenObjectsShouldNotBeSame() { + + Course course = new Course(1, "Spring Masterclass"); + Student student1 = new Student(1, "John", course); + Student student2 = new Student(2, "David", course); + + List students = new ArrayList<>(); + students.add(student1); + students.add(student2); + + List deepCopy = Student.deepCopyUsingCopyConstructor(students); + + Assertions.assertNotEquals(students.get(0), deepCopy.get(0)); + Assertions.assertNotEquals(students.get(1), deepCopy.get(1)); + } + + @Test + public void whenCreatingDeepCopyWithSerializationUtils_thenObjectsShouldNotBeSame() { + + Course course = new Course(1, "Spring Masterclass"); + Student student1 = new Student(1, "John", course); + Student student2 = new Student(2, "David", course); + + List students = new ArrayList<>(); + students.add(student1); + students.add(student2); + + List deepCopy = Student.deepCopyUsingSerialization(students); + + Assertions.assertNotEquals(students.get(0), deepCopy.get(0)); + Assertions.assertNotEquals(students.get(1), deepCopy.get(1)); + } + + @Test + public void whenCreatingDeepCopyWithJackson_thenObjectsShouldNotBeSame() { + + Course course = new Course(1, "Spring Masterclass"); + Student student1 = new Student(1, "John", course); + Student student2 = new Student(2, "David", course); + + List students = new ArrayList<>(); + students.add(student1); + students.add(student2); + + List deepCopy = Student.deepCopyUsingJackson(students); + + Assertions.assertNotEquals(students.get(0), deepCopy.get(0)); + Assertions.assertNotEquals(students.get(1), deepCopy.get(1)); + } +}