Override Equals method improvement (#14446)

This commit is contained in:
Shahul Basha 2023-08-20 02:25:42 -04:00 committed by GitHub
parent 9814ba18ae
commit 94ab13cacc
3 changed files with 56 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package com.baeldung.deepcopyarraylist;
import java.io.Serializable;
import java.util.Objects;
public class Course implements Serializable, Cloneable {
@ -39,4 +40,23 @@ public class Course implements Serializable, Cloneable {
throw new IllegalStateException(e);
}
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Course that = (Course) o;
return Objects.equals(courseId,that.courseId)
&& Objects.equals(courseName,that.courseName);
}
@Override
public int hashCode() {
return Objects.hash(courseId,courseName);
}
}

View File

@ -2,6 +2,7 @@ package com.baeldung.deepcopyarraylist;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.SerializationUtils;
@ -100,4 +101,23 @@ public class Student implements Serializable, Cloneable {
student.course = this.course.clone();
return student;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Student that = (Student) o;
return Objects.equals(studentId,that.studentId) &&
Objects.equals(studentName, that.studentName) &&
Objects.equals(course, that.course);
}
@Override
public int hashCode() {
return Objects.hash(studentId,studentName,course);
}
}

View File

@ -19,8 +19,10 @@ public class DeepCopyArrayListUnitTest {
List<Student> deepCopy = Student.deepCopyUsingCloneable(students);
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
Assertions.assertEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotSame(students.get(0),deepCopy.get(0));
Assertions.assertEquals(students.get(1), deepCopy.get(1));
Assertions.assertNotSame(students.get(1),deepCopy.get(1));
}
@ -37,8 +39,10 @@ public class DeepCopyArrayListUnitTest {
List<Student> deepCopy = Student.deepCopyUsingCopyConstructor(students);
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
Assertions.assertEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotSame(students.get(0),deepCopy.get(0));
Assertions.assertEquals(students.get(1), deepCopy.get(1));
Assertions.assertNotSame(students.get(1),deepCopy.get(1));
}
@Test
@ -54,8 +58,10 @@ public class DeepCopyArrayListUnitTest {
List<Student> deepCopy = Student.deepCopyUsingSerialization(students);
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
Assertions.assertEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotSame(students.get(0),deepCopy.get(0));
Assertions.assertEquals(students.get(1), deepCopy.get(1));
Assertions.assertNotSame(students.get(1),deepCopy.get(1));
}
@Test
@ -71,7 +77,9 @@ public class DeepCopyArrayListUnitTest {
List<Student> deepCopy = Student.deepCopyUsingJackson(students);
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
Assertions.assertEquals(students.get(0), deepCopy.get(0));
Assertions.assertNotSame(students.get(0),deepCopy.get(0));
Assertions.assertEquals(students.get(1), deepCopy.get(1));
Assertions.assertNotSame(students.get(1),deepCopy.get(1));
}
}