Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What will if two object has the same hashcode?

 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya You all are right.

I need to understand what a hashcode is used for?

Thanks for reply!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic