• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how hashCode() is used in HashMap?

 
ramu ta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how hashCode() is used in HashMap?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check Jesper's post here.
 
ramu ta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnak you, i understood the usage of hashCode() in HashSet.

but i want to know the usage of hashCode() in HashMap.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept is the same. The hashCode is used to identify in which bucket the object will be place in.
 
ramu ta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in HashSet, we can pass the searching object to the constructor of Hashset.
so the construtor is calling hashcode() method of searching object and going to the specific bucket for applying equals() method on those specific limited objects. this is clear to me...

but in HashMap.. we should pass key to get value... how this process is going? i m in confusion, will you please clear it?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
veda kakarla wrote:......but in HashMap.. we should pass key to get value... how this process is going? i m in confusion, will you please clear it?

In a HashMap, essentially all the keys are stored in a Set. So, the same concept as for Set applies here too. When you ask for a value by giving the key, hashCode() and equals() shall be used to identify the bucket in which the key is in. Once the key is located, the value can be retrieved in no time.
 
ramu ta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic