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 Hash code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hash code" Watch "Hash code" New topic

Hash code

Rakesh Sharma
Ranch Hand

Joined: Dec 29, 2000
Posts: 37
What is Hash code and how thay are useful ?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
A hashcode is a value that is generated by an algorithm that converts an object to a value based on the value of all of the variables in the object. This is usually done to give the object a value that can be stored in a hashTable and then looked up as needed.
When you provide the key for a value to the hashtable, the data structure generates a hash code and uses that as the means to look up the value when it is necessary to retrieve it. As long as the algorithm for generating the hash codes always generates the same value for the same key (two keys are equivalent if equals() says so) and distributes the codes as evenly as possible, the hashtable represents an efficient means of storing key-value pairs. The means of generating a key's hash code is by the hashCode() method, either the one from Object, or an overridden version from a subclass.

Two entries are not necessarily equal if hashCode() returns the same value. In fact, if many entries result in the same hash code value, this is usually the result of a poor hashing algorithm. If you've overridden the hashCode() method, you should redesign how you generate hash codes for instances of your class. For instance, the length of a String is a bad hashing algorithm, as many unequal strings would have the same code.
The operations you can do with a Hashtable are mostly inherited from Dictionary (or part of the new Map interface).
End quote

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Hash code
It's not a secret anymore!