This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is HashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is HashCode" Watch "What is HashCode" New topic
Author

What is HashCode

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
could someone please explain what hashcode is and what its utility in the api or how it is useful.
thank u
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
A hashcode is a number generated from any object. This is what allows objects to be stored/retrieved quickly in a Hashtable.
Imagine the following simple example:
On the table in front of you you have nine boxes, each marked with a number 1 to 9. You also have a pile of wildly different objects to store in these boxes, but once they are in there you need to be able to find them as quickly as possible.
What you need is a way of instantly deciding which box you have put each object in. It works like an index; you decide to find the cabbage so you look up which box the cabbage is in, then go straight to that box to get it.
Now imagine that you don't want to bother with the index, you want to be able to find out immediately from the object which box it lives in.
In the example, let's use a really simple way of doing this - the number of letters in the name of the object. So the cabbage goes in box 7, the pea goes in box 3, the rocket in box 6, the banjo in box 5 and so on. What about the rhinoceros, though? It has 10 characters, so we'll change our algorithm a little and "wrap round" so that 10-letter objects go in box 1, 11 letters in box 2 and so on. That should cover any object.
Sometimes a box will have more than one object in it, but if you are looking for a rocket, it's still much quicker to compare a peanut and a rocket, than to check a whole pile of cabbages, peas , banjos and rhinoceroses.
That's a hash code. A way of getting a number from an object so it can be stored in a Hashtable. In Java a hash code can be any integer, and each object type is responsible for generating its own. Lookup the "hashCode" method of Object.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanx,
it was really helpful. i would however like to know how hashcode is unique. for example the hashcode for "rahul" is unique and is 218714508. What use is this to the programmer.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
As mentioned above (see rocket and peanut) the hash code doesn't have to be unique, just mostly different. If a hashcode is too large, then it's "wrapped" down to the number of boxes in a hashtable using the '%' modulus operator (like rhinoceros).
So if you try to put your object with hash code 218714508 into a hashtable with 10 "boxes" 0..9, it will put it into box 8. If you try hard enough, you will eventually find another object with the same hashcode of 218714508, and they would both go into "box" 8. But then so would objects with hash codes 8, 18, 28 ...
This is why Hashtable has a constructor which takes an int specifying the initial number of "boxes". If you plan to put a lot of items in the Hashtable, you might consider specifying a larger number to reduce the average number of objects per "box".
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Frank i did not understand how the hash 218714508 will go in the box 8. the sum of the above numbers is larger and what is the limit to a hashcode number. how can it be useful in programming please a give me a example. thanks
pestodesto
Greenhorn

Joined: Aug 15, 2001
Posts: 1
The hash code itself is not useful to the programmer, if the string "Dog" becomes 291564382, then you can't convert that number back to get dog. As stated above several times, the number is generated to be as unique as possible, but is not necessarily guaranteed to be unique. The benefit to the programmer is that the hash code can be used in a hash table to be used as a lookup id, like an index. The hash table is like any other collection (for the most part), a group of objects, and some method of accessing those objects. Otherwise, the hash code provides no other utility to the programmer, except for some possible novel and rare situations that may occur.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in again with a new name which meets the requirements.
Thanks.
--Mark
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Wow, that was a very clear and memorable explaination, Frank. Reminds me of the time a professor for whom I TA'ed explained how binary counting works using an example of men's urinals. (Even better, it was during on the Friday of Parent's Weekend, so we had parents attending the class. :-)
--Mark
C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Check this out Frank!
http://forum.java.sun.com/thread.jspa?threadID=5200380
[ August 15, 2007: Message edited by: C Vivek ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
This thread is over 6 years old! Pointless replying now. DontWakeTheZombies


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Subhasish Tripathy
Greenhorn

Joined: Oct 27, 2013
Posts: 1

Now I got a good idea on hashcode, Really helpful. thank you frank.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch

Nice to know that even such old threads can be useful.
MohamadHosein Saeedi
Greenhorn

Joined: Nov 12, 2013
Posts: 1
@Frank Carver : thank you
its a perfect explaination of hashcode from 2001 , now that would be great if anyone explain the relation between hashcode method and equals...

best regards
MHS

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch

Have you read this about the hashCode method? If so, what needs explaining?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is HashCode
 
Similar Threads
what is hashcode and equals contract?
hash code
Hashcode
hashcode() in java
basicjava