Can some one kindly explain me the scenaro in real application when one needs to write equals() and hashcode() method . I heard we do it in hibernate's entity class or in test program. Can some one elaborate this.Thanks in advance
-
related: http://stackoverflow.com/questions/21148288/why-do-we-override-equals-and-hashcode-methods-how-does-hashcode-works-in?rq=1 – Jan 02 '15 at 06:40
-
In a nutshell, overriding the `hashcode()` and the `equals()` methods is essential, when objects of the target class are to be stored in hashed collections like `Hashtable` (obsolete), `HashMap`, `HashSet` stc. – Tiny Jan 02 '15 at 06:49
-
I know very well about hashing and imp of equlas() and hashcode() methods. I just want to know the real scenarios in a real time application when one needs to implement them.I know one scenario like for using as key in hash realted maps or when we are storing objects in hashset. Kindly give some other scenarios like - why in Hibernate entity object we need to overide these two methods? – Diana Jan 02 '15 at 06:54
-
It is not mandatory to override them in Hibernate entities, if you guarantee that objects of those entity classes are never going to be stored in a hashed collection and that you consider comparison between those objects to be superfluous based on the primary key throughout your application. – Tiny Jan 02 '15 at 07:02
-
Thanks and I fully agree but can you give me any scenario other than hashed related collection when we need to overide these methods(also other than running test program)? – Diana Jan 02 '15 at 08:39
2 Answers
One common reason is so that the Object
will work if we add it to a collection like a HashMap
. The reason this might be necessary should be deducible from the Javadoc for Object.hashCode()
which says (in part),
As much as is reasonably practical, the hashCode method defined by class
Object
does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
And the Object.equals()
Javadoc which says in part,
The equals method for class
Object
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returns true if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes.

- 1
- 1

- 198,278
- 20
- 158
- 249
Every class is given an equals()
and hashcode()
method.
The default equals()
method simply compares the memory address of the allocated object.
The default hashcode()
method returns the memory address of the allocated object.
The equals()
method should be overridden if you want to specify some less rigid criteria of equality (the default equals will only return true if the references are equal). For example, in the String
class, the equals method compares the strings char by char and returns true if every char is the same.
The hashcode()
method should be overridden if you intend to do any sort of hashing. I'm not sure how familiar you are with data structures, but hashing is of paramount importance in computer science. Some examples of data structures that use hashing are hash tables and dictionaries.

- 618
- 6
- 14
-
I know very well about hashing and imp of equlas() and hashcode() methods. I just want to know the real scenarios in a real time application when one needs to implement them.I know one scenario like for using as key in hash realted maps or when we are storing objects in hashset. Kindly give some other scenarios like - why in Hibernate entity object we need to overide these two methods? – Diana Jan 02 '15 at 06:51