开发者

Java override Object equals() method

开发者 https://www.devze.com 2022-12-21 11:24 出处:网络
How do I override the equals method in the object class? i.e I have class Person{ //need to override here

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 want to convert the parameter obj to a type Person, but开发者_C百科 if I do (Person) obj it won't work.


It's actually more complicated than you might think. Have Eclipse (or whatever IDE you're using) auto-generate an equals method; you'll see it contains a few checks and casts before it does a comparison.

Also see here: http://www.javapractices.com/topic/TopicAction.do?Id=17


You can cast it inside the method, just make sure that is of the right type using instance of

if(obj instanceof Person)
{
   Person otherPerson = (Person) obj;
   //Rest of the code to check equality
}
else
{
//return false maybe
}


@Override
public boolean equals(Object o) 
{
    if (o instanceof Person) 
    {
      Person c = (Person) o;
      if ( this.FIELD.equals(c.FIELD) ) //whatever here
         return true;
    }
    return false;
}


Take a look at Regarding Object Comparison.

Be aware that if you override equals() you must also override hashCode(). The equals/hashCode contract is that if two objects are equal they must have the same hash code.


If you plan to create subclasses of Person, use something like

if(obj!=null && obj.getClass() == Person.class)

rather than instanceof


The only reason to use getClass() rather than instanceof is if one wanted to assert that both references being compared point to objects of the exact same class rather than objects implementing the same base class.

Say we have an Employee e and a Manager m (extends Employee).

m instanceof Employee would yield true, m.getClass() == Employee.class would return false.

In some cases the latter might be preferred, but rarely in case of comparison of instances in equals() or hashCode() methods.


I prefer the simpler, null-safe(r) Objects.equals for any field type:

@Override
public boolean equals(Object o) {
    if (o instanceof Person) {
        Person p = (Person) o;
        return Objects.equals(p.FIELD, this.FIELD);
    }
    return false;
}


One more point may be good to know that after you override equals() method (and also hashcode()) method you can to compare two objects of same class like follows:

Person p1 = new Person();
Person p2 = new Person();

....

if ( p1.equals( p2 ) )

{
   // --- Two Persons are equal, w.r.t the fields you specified in equals method ---

}


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消