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 |
How to synchonise HashMap
Are global variables initialized to zero?
What is the relationship between clipping and repainting under awt?
How to stop a thread in java? Explain about sleep () method in a thread?
import java.io.*; class Demo { public static void main(String args[]) { File f=new File("1234.msg"); String arr[]=f.list(); System.out.println(arr.length); } }
Why are strings immutable in Java?
what is the significance of listiterator in java?
Explain about core java?
Can you run the product development on all operating systems ?
In what circumstances, compiler will supply a default constructor for a class?
What is jvm? Why is java called the platform independent programming language?
What is boolean strategy?