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.
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.
posted 18 years ago
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".
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
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.
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
posted 17 years ago
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
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.
posted 4 years ago
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.