How do I override the equals method in the object class?
i.e I have
class Person{
//need to override here
public boolean equals (Object obj){
}
I know this is answered, but in my travels I have found this the most efficient way to override the comparison of an object to make sure it happens the same globally:
@Override
public boolean equals(Object o) {
return o instanceof Person && this.getSomeMagicalField().equals(((Person) o).getSomeMagicalField());
}
or if you are not comparing strings:
@Override
public boolean equals(Object o) {
return o instanceof Person && this.getSomeMagicalField() == (Person) o).getSomeMagicalField();
}