Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is HashCode

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
could someone please explain what hashcode is and what its utility in the api or how it is useful.
thank u
 
Frank Carver
Sheriff
Posts: 6920
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is over 6 years old! Pointless replying now. DontWakeTheZombies
 
Subhasish Tripathy
Greenhorn
Posts: 1
Java Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got a good idea on hashcode, Really helpful. thank you frank.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47253
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Nice to know that even such old threads can be useful.
 
MohamadHosein Saeedi
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 47253
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you read this about the hashCode method? If so, what needs explaining?
 
Michael Ritchey
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Have you read this about the hashCode method? If so, what needs explaining?

I actually followed a link from stackoverflow.com after reading the java documentation. The info at oracle is useful but is a bit too technical to be a good answer to the OPs question. Thank you necropostingly, Frank, for a very good answer and others who also added good info.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47253
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Necropostingly? Welcome to the Ranch
Yes, it is a useful answer. To add to it, imagine what happens if you use a mutable reference type as a “K” in a Map. All is well until the “K” changes its state. If that changes the hash code so your Map is looking in a different bucket, you might never find the pair back.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic