• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion regarding hashCode() method of Object class

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have some queries:

a) What is a hashCode() of an object ? (is it the memory location at which it is stored?)

b) What is the significance of hashCodes with respect to how objects are stored in Collections?

~ Mansukh
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If only I had a dollar for every forum post about these questions!
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:a) What is a hashCode() of an object ?

It is a numeric digest that helps to identify it.

(is it the memory location at which it is stored?)

No.

b) What is the significance of hashCodes with respect to how objects are stored in Collections?

1. They only have significance for hashed collections (eg, java.util.HashMap).
2. Their effectiveness, in such a context, is directly proportional to how good they are; and that's a bit of a black art. Effective Java has a very good Item on how to write a good hash which doesn't blind you with maths.

For a more general description, you may also find this article useful.

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:a) What is a hashCode() of an object ?

It is a numeric digest that helps to identify it.

(is it the memory location at which it is stored?)

No.

b) What is the significance of hashCodes with respect to how objects are stored in Collections?

1. They only have significance for hashed collections (eg, java.util.HashMap).
2. Their effectiveness, in such a context, is directly proportional to how good they are; and that's a bit of a black art. Effective Java has a very good Item on how to write a good hash which doesn't blind you with maths.

For a more general description, you may also find this article useful.

Winston


Thanks man..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic