Java override method equals () method

How to override equals method in object class?

ie I have

class Person{ //need to override here public boolean equals (Object obj){ } 

I want to convert the obj parameter to type Person, but if I do (Person) obj, it will not work.

+6
java object generics
source share
9 answers

You can use it inside a method, just make sure it has the correct type using an instance

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

This is actually harder than you think. Eclipse (or any other IDE you use) automatically creates the equals method; You will see that it contains several checks and castings before it performs the comparison.

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

+10
source share
 @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; } 
+8
source share

Take a look at Object Comparison .

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

+5
source share

If you plan on subclassing Person, use something like

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

not instanceof

+3
source share

The only reason to use getClass() rather than instanceof is that I would like to say that both compared references point to objects of the same class, and not to objects that implement the same base class.

Say we have Employee e and Manager m ( Employee continues).

m instanceof Employee will give true, m.getClass() == Employee.class will return false.

In some cases, the latter may be preferable, but rarely if instances are compared in the equals() or hashCode() methods.

+2
source share

Another note may be useful to know that after overriding the equals() method (and also hashcode() ), you can compare two objects of the same class, for example:

 Person p1 = new Person(); Person p2 = new Person(); .... if ( p1.equals( p2 ) ) { // --- Two Persons are equal, wrt the fields you specified in equals method --- } 
0
source share

I know this is the answer, but in my travels I found this to be the most efficient way to override object comparisons to make sure it happens all over the world:

 @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
source share

I prefer simple, null (r) Objects.equals for any type of field:

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

All Articles