This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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.
 
Water! People swim in water! Even tiny ads swim in water:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!