5 Rules For Overriding Equal Method.
1) Reflexive : Object must be equal to itself. i.e. for any reference a, a.equals(a) should return true.
2) Symmetric: For any referneces a and b, if a.equals(b) is true then b.equals(a) must be returns true.
3) Transitive: For any references a, b and c, if a.equals(b) is true and b.equals(c) is true then c.equals(a) must be returns true.
4) Consistent: For repeated calls to a.equals(b) should consistenly return a true or consistently return a false, i.e multiple invocations of equals() method must return the same value until any of properties are modified. So if two objects are equals in Java they will remain equals until any of their property is modified.
5) Null comparison: comparing any object to null must be return false i.e. a.equals(null) should return false, and should not result in NullPointerException. Passing unknown object, which could be null, to equals in Java is is actually a Java coding best practice to avoid NullPointerException in Java.
Example:- Overriding Equal Method and hashCode()
1) Reflexive : Object must be equal to itself. i.e. for any reference a, a.equals(a) should return true.
2) Symmetric: For any referneces a and b, if a.equals(b) is true then b.equals(a) must be returns true.
3) Transitive: For any references a, b and c, if a.equals(b) is true and b.equals(c) is true then c.equals(a) must be returns true.
4) Consistent: For repeated calls to a.equals(b) should consistenly return a true or consistently return a false, i.e multiple invocations of equals() method must return the same value until any of properties are modified. So if two objects are equals in Java they will remain equals until any of their property is modified.
5) Null comparison: comparing any object to null must be return false i.e. a.equals(null) should return false, and should not result in NullPointerException. Passing unknown object, which could be null, to equals in Java is is actually a Java coding best practice to avoid NullPointerException in Java.
Example:- Overriding Equal Method and hashCode()
public class Person {
private int id;
private String firstName;
private String lastName;
private String email;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public boolean equals(Object obj) {
// null check
if (obj == null) {
return false;
}
if (!(obj instanceof Person)) {
return false;
}
/*
* if (obj.getClass() != this.getClass()) { return false; }
*/
// this instance check
if (obj == this) {
return true;
}
Person objPerson = (Person) obj;
// instanceof Check and actual value check
if ((obj instanceof Person) && (objPerson.getId() == id
&& (objPerson.getFirstName() != null && objPerson.getFirstName().equals(firstName))
&& (objPerson.getLastName() != null && objPerson.getLastName().equals(lastName))
&& (objPerson.getEmail() != null && objPerson.getEmail().equals(email))
&& (objPerson.getPhone() != null && objPerson.getPhone().equals(phone)))) {
return true;
} else {
return false;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
return result;
}
}
public class PersonTest {
public static void main(String[] args) {
Person person1 = new Person();
person1.setId(1);
person1.setFirstName("Amit");
person1.setLastName("Kumar");
person1.setPhone("123456789");
person1.setEmail("amit@test.com");
Person person2 = new Person();
person2.setId(1);
person2.setFirstName("Amit");
person2.setLastName("Kumar");
person2.setPhone("123456789");
person2.setEmail("amit@test.com");
System.out.println(person1.equals(person1));
System.out.println(person1.equals(person2));
}
}
Output:
true
true
No comments:
Post a Comment