How to override equals() and hashCode() method in java?
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Employee emp = (Employee) obj;
return id == emp.id
&& (firstName == emp.firstName
|| (firstName != null && firstName.equals(emp.getFirstName())))
&& (lastName == emp.lastName || (lastName != null && lastName .equals(emp.getLastName())));
}// equals method ends
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 :frstName.hashCode());
result = prime * result + id;
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;
}// hashCode method ends
| Is This Answer Correct ? | 0 Yes | 0 No |
Can an interface extend a class?
What is an example of a boolean?
What are some examples of variable costs?
Which list does not allow duplicates in java?
What method is used to specify a container's layout in java programming?
What is the difference between arraylist and hashset in java?
What is the association?
What is a parameter in a function?
What is the immediate parent class of the Applet class?
i need income tax program using java inheritance concept. could u please help me?
What are 4 pillers of object orinted programming?
What is supplier in java?