* Deep copy of Arraylist

* Deep copy of Arraylist

* Deep copy of Arraylist updated as per review
This commit is contained in:
Shahul Basha 2023-07-04 08:35:53 -04:00 committed by GitHub
parent de3fc4f21a
commit 1c12d45700
3 changed files with 222 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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<Student> deepCopyUsingJackson(List<Student> students) {
return students.stream()
.map(Student::createDeepCopy)
.collect(Collectors.toList());
}
public static List<Student> deepCopyUsingCloneable(List<Student> students) {
return students.stream()
.map(Student::clone)
.collect(Collectors.toList());
}
public static List<Student> deepCopyUsingCopyConstructor(List<Student> students) {
return students.stream()
.map(Student::new)
.collect(Collectors.toList());
}
public static List<Student> deepCopyUsingSerialization(List<Student> 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;
}
}

View File

@ -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<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
List<Student> 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<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
List<Student> 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<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
List<Student> 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<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
List<Student> deepCopy = Student.deepCopyUsingJackson(students);
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
}
}