Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

hashcode()  RSS feed

 
Ranch Hand
Posts: 193
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain to me or point to a tutorial that gives a more detailed explaination to how hashcode() method is used in combonation with a hash collection? Thanks.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Hashing and hash codes from Thinking in Java.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my analogy...

Suppose you're looking for a particular book in a library. One approach would be to start with the first book on the first shelf and work your way through the entire library until you find your book. But it would be a lot quicker if you knew which asile contained your book, and you could narrow your search to that area. It would be even quicker if you knew which shelf your book was on.

In effect, hashcode narrows your search to a particular "bucket" or "slot" associated with that hashcode. There might be more than one element in that slot, because unequal objects are not required to return different hashcodes. But if the hashing is good, there shouldn't be too many elements in any one slot. In other words, a book's hashcode would do better to return a shelf number rather than a asile number.

This also illustrates why equal objects must return the same hashcode. If you have multiple copies of the same book, you would expect to find them on the same shelf.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Marc
wonderful explanation sir...
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For best and easy study material about hashCode()
refer to K & B Head First Java 2nd edition .

In Collection chapter , you will find short , simple and easy
tutorial about hashCode() and equals() method .
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah Marc,
That was a great analogy.
It sure makes life ( a Lot) easier when these topics are compared to simple real life time situation instead of codes !

Thanks.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!