• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why override equals() method ?  RSS feed

 
Sanjeev Kulkarni
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've seen code where equals() has been overridden and so is hashCode().
Can anyone please explain me why is there a need of overrdiing the equals() with an example ?

Thanks
Sanjeev
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadocs for java.lang.Object class, equals and hashCode methods contain an excellent discussion. See also the use of the equals method in the discussion of the java.util.Map interface.
Bill
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Equals method that is defined in the Object class is the default implementation of equals for all the other objects.That basically compares the two objects in the heap and true if they are the same or else false.

Suppose you have made a new class and want to use it as the key for any of the implementations of Map interface.Then the Map would use the hashcode of the object/key to decide the bucket where the value should go and also the equals needs to be true in that case.So you have to override equals too for this.There are also some properly to be satisfied in case you override these methods.Check the javadoc for that..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
Then the Map would use the hashcode of the object/key to decide the bucket where the value should go


This is only true for the hash-based collections like HashMap and HashSet. The hashCode() method isn't used by TreeMap, TreeSet, EnumMap, IdentityHashMap, etc.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
The Equals method that is defined in the Object class is the default implementation of equals for all the other objects.That basically compares the two objects in the heap and true if they are the same or else false.


If you are comparing two objects on the heap then the equals() method of the Object class will always return false. Object.equals() is equivalent to using ==. It will only return true if both references are pointing to the same object.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is true , its the reason why I have asked to override the equals method when you want to objects to be equal satisfying certain crietria.Like I have a class having one String variable as name and other int variable as age and I want the objects of this class to be equal in case the name and age of the two objects are equal.So I have to override the method so that it returns true if name and age of both the objects are same.

Apart from this there are other certain conditions that are to be satisfied is case you override the equals method(reflexive , transitive,symetric {mentioned in the method level javadoc for equals method}).

If the hashcode of two objects are same then equals operation performed in the same objects should also be true.The reason is logically hashcode is calculated from the memory address of the object in the heap and when hashcode is same means that both the references are pointing to the same memory in the heap so the equals should also be true.This is the general contract between hashcode and equals
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
If the hashcode of two objects are same then equals operation performed in the same objects should also be true.


This is not actually a requirement (two unequal objects can return the same hashcode), but the reverse is. If the equals() operation shows two objects to be equal, then the hashcode() method of both objects must return the same value.
[ September 19, 2006: Message edited by: Joanne Neal ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah , that is true.I worte it the other way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!