Re: do I need to override the equals() method?



Marteno Rodia a écrit :
I've defined a class which, of course, silently extends
java.lang.Object, but I've added some my own attributes. Now I want to
be able to compare two objects of my class i.e. to check if they are
identical. Do I need to write my own equals() method or can I use the
inherited java.lang.Object.equals() method?

Albert wrote:
You could have searched a little...

What makes you think he didn't?

Object.equals() only compare reference equality, which means it returns true for exact same object

so if you want that equals returne true for "content identical" object, yes you should override the equals(Object) method.

It depends on your uses. If you put object in collections and use contains() for example, you might want to override equals.

Joshua Bloch covers this in /Effective Java/, a book you must own.

Always override 'equals()' and 'hashCode()' together if at all. Make sure they are consistent with each other, i.e., if two instances compare equal, they must have the same hash code. (The converse is not true.) Make sure you involve each field that logically affects value equality.

For example, in comparing instances of a hypothetical 'Person' class, you might involve 'nationalID' (e.g., Social Security number). Or you might compare a combination of 'birthName', 'birthDate', 'birthPlace', 'birthGender' and 'disambiguator'. The same field(s) would be involved in both the 'equals()' and 'hashCode()' methods.

--
Lew
.



Relevant Pages

  • RE: Equality vs Sameness
    ... Equals would not offer any benefit. ... >> from it in the future if the users haven't asked for that functionality. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help! Need to sorted collection accessible by key
    ... Equals() and GetHashCode, then return the hashcode of the key you want ... access with, and compare by the one you want to order by, and put it ... If two items that compare equal using the _hasher key do not compare ... hash to the same bucket in the hash table but will not be equal to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Interesting list Validity (True/False)
    ... Python to compare those strings equal. ... "a equals d" is true. ... There may or may not be any coercion involved.) ... And if you ask any mathematician, he'll say that is equal to. ...
    (comp.lang.python)
  • Re: Reading specific memory address into variable
    ... cons_pi and it equals 3.14159265358979. ... for how many times they want to do that calculation to figure out pi (4/1 - ... which is where I was stuck trying to figure out a way to compare the two ... incrementing the accuracy variable by 1 and incrementing the multiply ...
    (comp.lang.cpp)
  • Re: do I need to override the equals() method?
    ... And, of course, if you override equals(), you must override ... Note that there _IS_ a risk of being too liberal with equality. ... compare as equals, then they will replace each other in sets and map keys. ...
    (comp.lang.java.programmer)