File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes hashcode()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashcode()?" Watch "hashcode()?" New topic


Shyam Purkayastha
Ranch Hand

Joined: Jan 15, 2001
Posts: 57
Hi all
What is the use of hashcode() function?
Is it related to hashtable class ?
please help
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I am not completely sure, but I think you are on the right track. AFAIK, the HashTable and related classes from the Container API use the hashcode() function when it stores an object.

Java API Documentation
The Java Tutorial
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It is used by any class that stores objects by using the hash code.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
A hashcode is usually used as an efficient way of searching for stored objects.
Say you have a list of 1000 names, it would take a reasonable amount of time to search for the name "Jeremy Thornton" in the list.
If however you stored the names according to their hashed value (which is what the HashX type containers do) you just search for the hash of "Jeremy Thornton" - an integer - instead.
As integer processing is much faster than string processing the search is much quicker.
Hoper this helps,
Shyam Purkayastha
Ranch Hand

Joined: Jan 15, 2001
Posts: 57
thanks a lot jeremy
It is sorta covered in the JavaRanch Style Guide.
subject: hashcode()?
It's not a secret anymore!