• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashcode for HashMap not dependent on the hashcode of key added?  RSS feed

 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was under the impression that hashcode generated for keys is dependent on the hashcode of the object being added as key
But the following program has no effect on hashcode by writing the customer hashcode for the class

Employee.java



EmployeeHashCodeTest .java



Can someone explain please?
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essentially you are asking why the hashcode of the string "Second" doesn't change when you declare a hashCode() method in your Employee class.

Remember that the get() method of a Map returns the value, not the key. Calling it "the key" in your output doesn't make it the key.
 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I realized that
So, how do I get the hashcode for the keys used by hashmap for mapping values
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steps to get hashcode for keys -
a.)Get keys (by using keys() method)
b.)invoke your hashcode method on them.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!