Possible Duplicate:
Overriding equals and hashCode in Java
I am supposed to implement hashcode and equals for Custom class Person. Person consists of
firstname
lastname
I am supposed to implement equals and hashcode such that two people with firstname and lastname should return true for equals and should be accepted by Hashmap. I have implemented Person class like this:
public class Person {
String firstname;
String lastname;
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return firstname.hashCode()+lastname.hashCode();
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
Person u=(Person) obj;
return u.firstname.equals(firstname) && u.lastname.equals(lastname);
}
}
Is the implementation of Hashcode correct here? Even though I am getting the expected result,I want to know if this implementation is correct?