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 |
Give us a program to check for parenthesis matching using stack.
Are private methods final?
Which of the following is not an isolation level in the JDBC
Which api is provided by java for operations on set of objects?
What is the purpose of tostring() method in java?
What Method and class used for Connection pooling ?
What is serialization in java?
What are the restriction imposed on a static method or a static block of code?
Why a dead thread occurs?
Explain about instanceof operator in java?
State the difference between strings and arrays.
What is a Null object?