From 94ab13cacc769c99a54f5a5dbae1f073a978ee1c Mon Sep 17 00:00:00 2001 From: Shahul Basha <32072554+shahulbasha@users.noreply.github.com> Date: Sun, 20 Aug 2023 02:25:42 -0400 Subject: [PATCH] Override Equals method improvement (#14446) --- .../baeldung/deepcopyarraylist/Course.java | 20 ++++++++++++++++ .../baeldung/deepcopyarraylist/Student.java | 20 ++++++++++++++++ .../DeepCopyArrayListUnitTest.java | 24 ++++++++++++------- 3 files changed, 56 insertions(+), 8 deletions(-) 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 index f8367cfd50..75edca087f 100644 --- 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 @@ -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); + } } 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 index 0b3f1ba4a9..ce9f773bc7 100644 --- 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 @@ -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); + } } 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 index 625c7e8385..d7e4c0ff64 100644 --- 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 @@ -19,8 +19,10 @@ public class DeepCopyArrayListUnitTest { List 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 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 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 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)); } }