File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashCode Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashCode Implementation" Watch "HashCode Implementation" New topic
Author

HashCode Implementation

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
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


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Arvind Porlekar
Ranch Hand

Joined: Dec 31, 2008
Posts: 38
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

Joined: Dec 20, 2007
Posts: 299
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

Joined: Dec 31, 2008
Posts: 38
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

Joined: Jan 22, 2007
Posts: 400

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


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Arvind Porlekar
Ranch Hand

Joined: Dec 31, 2008
Posts: 38
Thats we want...Map Allow unique Key and Set allow unique value.

Hope you clear Hash Code and Equals.


--Arvind
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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

Joined: Jan 22, 2007
Posts: 400



Try the above code as it is, then try after removing comments into model class.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Minhaj and Arvind...I got it... Thanks again for so much effort ... :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashCode Implementation