• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is HashCode

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Likes 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread is over 6 years old! Pointless replying now. DontWakeTheZombies
 
Greenhorn
Posts: 1
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I got a good idea on hashcode, Really helpful. thank you frank.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Nice to know that even such old threads can be useful.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you read this about the hashCode method? If so, what needs explaining?
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of minutes ago, What is hashcode? I was like Now, Hashcode hmm Thank you Frank
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody ;)

I'd like to say thanks to Frank - this is definitely the oldest forum entry that ever helped me a lot!!! / 17 years ;)

Kind regards
Joe / Vienna, Austria, Europe
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JS, welcome to the Ranch

Yes, I think you have foun the oldest post I have seen re‑used.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gee, I just wanted to know what hash code is. Can't believe I found a thread that is 18 years old. And someone 10 years ago thought the thread was old!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is a mere teenager. We used to worry about old threads, but this sort of thing shows that they can still be useful.

And welcome to the Ranch
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to say, the explanation is so helpful. Thanks a lot, Frank.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and thank you, nearly twenty‑year‑old post.
 
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank Carver...I liked the explanation of HashTables.
Thanks,
Kevin
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If hashcode is only helpful in HashTable, HashSet and HashMap. Why should all the objects(right from String, Integer, User defined objects., etc.,) in java should have the hashCode value with it?
Can some one explain me.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the Java designers thought it would be a good idea to make all objects usable as keys in hash maps, or elements in hash sets.

Maybe that was a mistake. Maybe they should have made a separate interface Equatable and a separate interface Hashable that extends Equatable.

They didn't though, so here we are.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vignesh Mani wrote:. . . . Why should all the objects(right from String, Integer, User defined objects., etc.,) . . . have the hashCode . . . ? . . ..

Remember that String and Integer are popular as “K”s in hash maps, because they are commonly used and also immutable.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . separate interface Equatable and a separate interface Hashable that extends Equatable. . . .

As they probably should have done with Cloneable. At least all objects have fully‑implemented equals() and hashCode() methods.
* * * * * * * * * * * * * * *
They could have called one of those interface Equable which is naming just as good as this interface, which always makes me think the police should get involved
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just here to make sure this thread lives on <3. Didn't read it even though i still really should learn what hashcode is
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I continue to see many examples per week where people teaching others show user-defined types without overriding .equals() let alone .hashCode(), despite putting getters and setters for every data member.

Yes, they definitely go on to put them in collections!  Ugh!
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic