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 equals hashcode doubt sjcp 1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals hashcode doubt sjcp 1.4" Watch "equals hashcode doubt sjcp 1.4" New topic
Author

equals hashcode doubt sjcp 1.4

mathew jonson
Greenhorn

Joined: Dec 06, 2007
Posts: 9
Hello,

I have a doubt with equals and hascode methods. I test this code.



If two objects are equal, their hashCodes must return the same integer. In this example the hascode isn't the same, because it isn't implemented and it returns the hashCode of class Object, that always returns diferents hashcode for diferent objects.

I don't understand why this code works and why the hashMap is able to find the objects if the last is true. If the contract is only valid for hash, why does this code works?

And if I don't want to use the TestEqualsHashcode class objects to use in keys of hash, why I must override hashCode if I override equals? The code compile and works


Tanks in advance. Kind regards
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
I've been studying all day and my head hurts so I haven't put much effort on your code, but I did run and look at it quickly. I'm not quite sure what's unpredictable to you, but you create objects using 'new' operator:
new TestEqualsHashcode(1) and even though they have the same constructor argument, they are in fact unique objects given that each one is using the new to create, well, a new object in memory. So each one will have a unique hashcode representation which uniquely identifies each key for the map. If you look at the default implementation of hashcode method in object you'll see that it basically creates a hashcode based on converting the memory address to an int hashcode. So when you print those out it shows the hashcodes like you'd expect. And you were able to get the correct elements back because you had unique hashcode created by object's hashcode method. However, your equals method tests instanceof and num. All is good so you get true for all of them. But what if you create a new element by copying the object, changing the 'num' and then adding it to the map? Check out this code from your example. Note the bold part and then trace the code and run and the result is much different because you can't depend on unique hashcode from Object's implementation of hashcode. I should have also done one where the num stays the same but I'll let you play with that LOL. I think you'll javascript: x()get better replies if you keep your code snippets a bit shorter by the way (sorry if I can't be more helpful but I'm too tired to think hard LOL):


[ December 13, 2007: Message edited by: nico dotti ]
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
Here's another run at it:

mathew jonson
Greenhorn

Joined: Dec 06, 2007
Posts: 9
Hi Nico, I'm grateful for your efforts and I understand you have said. But I have no clear the concept yet. There are a lot of question like that: "if you override equals, you MUST override hashcode", and the only one reason for this
is to have a good hash. The MUST word, may be changed by this: "If you override equals, and if you want to use the objects as a key in a hash collection, then override properly hashcode".
The only diference that I have found without override hashcode is that internally, I have a non optimized hash. Am I wrong?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals hashcode doubt sjcp 1.4
 
Similar Threads
Detecting Duplicates in Hashmap
Do I have to override equals() due to using an object as a hashmap key? Yes..but..
HashMap Duplicate Element Problem
Problem in implementing a TreeSet
Please Explain (Hashcode)