• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What will if two object has the same hashcode?

 
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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@@
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya You all are right.

I need to understand what a hashcode is used for?

Thanks for reply!
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic