Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashCode Implementation

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not exactly sure when do we need to implement equals() and hashCode() method? As far as I Understand, if we use a HashMap, and put some object of user defined class as key, then that class must implement both of the above methods or else we wont be able to retrieve the object for that key....







Above, i use an instance of class HashCodeExampleSimple as key in map.However, I dont see any issues when i try to retrieve the value based on the above keys...

Then please let me know when can the problem occurs and when do i need to implement hashcode() and equals() method
 
Arvind Porlekar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/475471/Programmer-Certification-SCJP/certification/hashCode-equals-method-test

hope that will help you..!!!

See..if two objects are equal then their hash code must be equal..thats why we are overrides hash code method when you overrides equals method.

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks arvind but that doesnot exactly asnwer my question.....
What i want to know is that what if i dont implement any of these methods...like in the example mentioned in my first post...
I read that , every key in HashMap must implement both the methods(If iam interested to retrieve the object i.e value). I need more info on that
 
Arvind Porlekar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Your example you are not overwriting any equals and hash code method.

You are creating three instance of HashCodeExampleSimple and putting into the Map....But what if any of two instances are equal.?
From our code they will added into the Map...that should not happen...!!!

thats why we are checking into the Equals that Before adding into the Map/Set they are equal OR not...also check the hash code ...
in hash code we are generating unique number that will same if two objects are equals ...thats why that equal object will not get added into Map or Set..


AS far your code what should happen if i change Code like this..at line No 17



Does map allowed to add ...?

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maan Suraj wrote:
Above, i use an instance of class HashCodeExampleSimple as key in map.However, I dont see any issues when i try to retrieve the value based on the above keys...

Then please let me know when can the problem occurs and when do i need to implement hashcode() and equals() method



Implement hashCode() and equals() method into HashCodeExampleSimple class and then try the above code. (it will print 1 instead of 2).
let me know if you need further explanation.

Minhaj
 
Arvind Porlekar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats we want...Map Allow unique Key and Set allow unique value.

Hope you clear Hash Code and Equals.


--Arvind
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvind Porlekar wrote:Thats we want...Map Allow unique Key and Set allow unique value.

Hope you clear Hash Code and Equals.


--Arvind


yes off course its clear to me, both are different things. However, what is your doubt??!!!
are you still not clear??
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try the above code as it is, then try after removing comments into model class.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Minhaj and Arvind...I got it... Thanks again for so much effort ... :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic