• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashcode()

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Hashing and hash codes from Thinking in Java.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Marc
wonderful explanation sir...
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic