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



Mike Schilling wrote:
"Lew" <noone@xxxxxxxxxxxxx> wrote in message news:gr546m$s00$1@xxxxxxxxxxxxxxxxxxxx
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.

It occurs to me that something I'd like in an IDE is for it to let me mark the fields that consititute my unique key, and then it can generate equals() and hashCode() . The process is mechanical enough for the result to be at least 90% correct.

Eclipse has a hashCode and equals builder. It brings up a dialog that
lists the local variables with check boxes, and also gives an option to
use either instanceof or class object identity to test for class equality.

I like the fact that it deals with the two methods together, and that
you only get to pick variables once for both methods, reducing the risk
of inconsistent equals and hashCode.

Patricia
.



Relevant Pages

  • Re: hashCode() for Custom classes
    ... hashCode() is an aid to equals(). ... Instances from different classes will never compare as equal, but it would improve performance to ensure that they also don't have hash codes that collide. ...
    (comp.lang.java.programmer)
  • 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: equals and hashCode
    ... I understand the purpose of overriding equals() in comparing the data of an object verses the reference, but the requirement to override hashCode() is a little less clear. ... The simplest way to create an equals and hashCode implementation for an object would be to make a distinct hash code based on the value of the data in the object, then in equals, simply compare the value of hash codes. ...
    (comp.lang.java.programmer)