aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "hashcode()" Watch "hashcode()" New topic
Author

hashcode()

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See Hashing and hash codes from Thinking in Java.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
@Marc
wonderful explanation sir...


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
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 .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: hashcode()
 
Similar Threads
what is hashcode and equals contract?
need help with the space-time continium
Practice Question
hash code
basicjava