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 |
What is a blocking method in Java?
Is it compulsory for a try block to be followed by a catch block in java for exception handling?
what are the jsp tags with example?
What is file class and what is its purpose?
What is the difference between Trusted and Untrusted Applet ?
What are basic data types?
From the two, which would be easier to write: synchronization code for ten threads or two threads?
How can you say HashMap is syncronized?
What does file separator do in java?
Detail discussions on JVM, memory management and garbage collector.
What are the topics in core java?
public class Base { public void myMethod(int a,intb) {} } // Uses myMethod and then hides it. public class DerivedOne extends Base { private void myMethod(int a,int b); } will this compile or not .yes or no. why