Override Equals method improvement (#14446)
This commit is contained in:
parent
9814ba18ae
commit
94ab13cacc
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.deepcopyarraylist;
|
package com.baeldung.deepcopyarraylist;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Course implements Serializable, Cloneable {
|
public class Course implements Serializable, Cloneable {
|
||||||
|
|
||||||
|
@ -39,4 +40,23 @@ public class Course implements Serializable, Cloneable {
|
||||||
throw new IllegalStateException(e);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.baeldung.deepcopyarraylist;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.lang3.SerializationUtils;
|
import org.apache.commons.lang3.SerializationUtils;
|
||||||
|
@ -100,4 +101,23 @@ public class Student implements Serializable, Cloneable {
|
||||||
student.course = this.course.clone();
|
student.course = this.course.clone();
|
||||||
return student;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,10 @@ public class DeepCopyArrayListUnitTest {
|
||||||
|
|
||||||
List<Student> deepCopy = Student.deepCopyUsingCloneable(students);
|
List<Student> deepCopy = Student.deepCopyUsingCloneable(students);
|
||||||
|
|
||||||
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
|
Assertions.assertEquals(students.get(0), deepCopy.get(0));
|
||||||
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
|
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);
|
List<Student> deepCopy = Student.deepCopyUsingCopyConstructor(students);
|
||||||
|
|
||||||
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
|
Assertions.assertEquals(students.get(0), deepCopy.get(0));
|
||||||
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
|
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
|
@Test
|
||||||
|
@ -54,8 +58,10 @@ public class DeepCopyArrayListUnitTest {
|
||||||
|
|
||||||
List<Student> deepCopy = Student.deepCopyUsingSerialization(students);
|
List<Student> deepCopy = Student.deepCopyUsingSerialization(students);
|
||||||
|
|
||||||
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
|
Assertions.assertEquals(students.get(0), deepCopy.get(0));
|
||||||
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
|
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
|
@Test
|
||||||
|
@ -71,7 +77,9 @@ public class DeepCopyArrayListUnitTest {
|
||||||
|
|
||||||
List<Student> deepCopy = Student.deepCopyUsingJackson(students);
|
List<Student> deepCopy = Student.deepCopyUsingJackson(students);
|
||||||
|
|
||||||
Assertions.assertNotEquals(students.get(0), deepCopy.get(0));
|
Assertions.assertEquals(students.get(0), deepCopy.get(0));
|
||||||
Assertions.assertNotEquals(students.get(1), deepCopy.get(1));
|
Assertions.assertNotSame(students.get(0),deepCopy.get(0));
|
||||||
|
Assertions.assertEquals(students.get(1), deepCopy.get(1));
|
||||||
|
Assertions.assertNotSame(students.get(1),deepCopy.get(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue