• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion regarding hashCode() method of Object class

 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
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: 992
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
Posts: 10573
65
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: 1163
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..
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!