aspose file tools
The moose likes Java in General and the fly likes Overriding hashCode() and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Overriding hashCode() and equals()" Watch "Overriding hashCode() and equals()" New topic
Author

Overriding hashCode() and equals()

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 201

Hi all,
I have a doubt regarding hashCode() and equals() override.
Why is it important that we override hashcode() if we override equals()?I mean,hashCode() refers to memory address of the object,and two objects can be 'equal' even if they are not at the same memory location?
Please be patient with my ignorance and help me.

Thanks...
Lanny Gilbert
Ranch Hand

Joined: Jun 11, 2002
Posts: 103
If you override the equals(), you MUST also override hashCode(). Otherwise a violation of the general contract for Object.hashCode will occur, which can have unexpected repercussions when your class is in conjunction with all hash-based collections.

You can find more excruciating detail in the Java Language Specification, but suffice it to say that if you override equals(), you most override hashCode() because the
language designers say so
marlajee Borstone
Ranch Hand

Joined: Jun 26, 2008
Posts: 35
But I am not sure why we need at all to override the equals() and hascode() methods in any class; because if we look into this example:-
Say we have a class which has a String variable as isbn which needs to be set by passing through constructor.
ie.

This code segment will display objects are equal even though firstBook and secondBook reference two distinct objects. They are considered equal because the objects compared contain the same value.

By overriding the equals() and hascode() method we do nothing new rather than generating a hashcode which uses all the variables values used in that object. So that if that object is compared with any other object, the hashcode() will be check and if it is found as same then they are decided to be equal.

So, why do we need to bother to override this equals() and hascode(), why just follow the process as shown above for firstBook and secondBook ?

Please help to get the clear reasoning regarding this above example in context with overriding equals() and hascode().

I will appreciate your input...

regards,
Dhannsumal !!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

marlajee Borstone wrote:This code segment will display objects are equal even though firstBook and secondBook reference two distinct objects. They are considered equal because the objects compared contain the same value.

That depends on the implementation of class Book. With the following implementation they are not equal:
You have to override equals (and therefore also hashCode) to specify what equality means. In your example you assumed that Book equality means ISBN equality, but you do have to implement that first.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

@Sudhanshu

There are very good reasons behind overriding equals() and hashCode() in a custom class.

Try to search some reference on how HashMap and HashSet work with custom objects. This will give you an insight on exactly why (and how) one needs to implement equals() and hashCode() in a custom class, in order to get desired results with Hash-based collection classes. It's always good to know the reasons behind the rules of the language, as opposed to take them on face value. You tend to appreciate them more if you know why those rules were put up in the first place. (For example, why can't an overriding method throw a checked exception not thrown by overriding method?)

@marlajee
As Rob explained, the Book class may contain properties ISBN, Title, Authors etc. Which property should be used for equals() comparison still needs to be implemented within the class, else the 2 objects will not be deemed equal in your example.
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 285

have a look at the way hascode has been implemented in Object, the default is to convert the memory address of the object into an int and use that. This means that if you override the equals method only and not hascode, calling hashcode on the 2 objects will return different numbers. If you then put these into a hashed collection you won't be able to retrieve them...
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Overriding hashCode() and equals()
 
Similar Threads
LinkedHashSet
if we override hashCode method is compulsary to override equals method and viseversa.
How to remove duplicate elements form an ArrayList?
how to solve this hashcode() example.?
enums