aspose file tools*
The moose likes Java in General and the fly likes What will if two object has the same hashcode? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What will if two object has the same hashcode?" Watch "What will if two object has the same hashcode?" New topic
Author

What will if two object has the same hashcode?

Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 211



Hi all,

I have a question that if there are two objects of HashMap and their hashcode is same what will happen?

Its an interview question. Which I have faced.





OutPut is :


I am not to clear it. what's happening behind it.

Thanks@@
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1779
    
    7

In AbstractMap:


In HashMap.Entry:


So, same entries, same hashCode() result.

However, as an interview question this makes little sense.
Are you sure the question was not about the behavior of HashMap when two keys share the same hashCode() value?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Rd Dari wrote:I have a question that if there are two objects of HashMap and their hashcode is same what will happen?

Simple answer: They will be be put in the same "bucket", providing that the two objects you talk about are KEYS, not VALUES.

What is a bucket? Like Jelle said: you need to do some more reading.

Specifically, one of the things you need to understand is what a Map is: it's a relation between a KEY and it's VALUE.
And in Java, it is a 1:1 relationship (ie, a key can have only ONE value) - and in a HashMap, it's the KEY that is hashed, NOT the value.

Sorry for all the caps, but it's important stuff; if you don't understand that, then the business of understanding HashMaps is going to be difficult.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 211

Ok

But in this example the output is to confused to me about the calculation





Specifically, one of the things you need to understand is what a Map is: it's a relation between a KEY and it's VALUE.
And in Java, it is a 1:1 relationship (ie, a key can have only ONE value) - and in a HashMap, it's the KEY that is hashed, NOT the value.


how to be count this output.
Output is :

Please clear my all doubt I am not able to understand this calculation by myself.

Thank you all of you in advanced!
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1779
    
    7

Well, given the hashCode() implementation of HashMap.Entry and the fact that the hashCode() of Integer simply returns its value, you should be able to figure that out.
However, it's really not worth worrying too much about the implementation details of how a hashCode() method calculates its return value, unless you're implementing your own hashCode() method. Understanding the contract of the hashCode() method, how it relates to the equals() method and how it's used by hash-based collections - like HashSet and HashMap - is much more important.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Rd Dari wrote:Please clear my all doubt I am not able to understand this calculation by myself.

I'm not surprised; neither do I.
it.next()
returns a Map.Entry, so
it.next().hashCode()
returns the hash code of the Entry; and according to the docs that is an exclusive-OR of the hashcodes for the key and the value, so
h += it.next().hashCode();
could be almost anything.

I'm with Jelle here: Don't obsess too much about specific calculations. First, understand what a hashcode is used for.

Winston
Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 211

ya You all are right.

I need to understand what a hashcode is used for?

Thanks for reply!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What will if two object has the same hashcode?