Java Reference
In-Depth Information
Computing a hash code is a one-way process. Getting the original piece of information from a hash code is not an
easy task and it is not the goal of the hash code computation either.
The piece of information that could be used to generate a hash code could be an arbitrary sequence of bytes,
characters, numbers, or a combination of them. For example, you may want to compute the hash code for a string
Hello ”.
What does a hash function look like? A hash function may be as simple as the following function, which returns
the integer zero for all input data:
int myHashFunction(your input data) {
return 0; // Always return zero
}
The above hash function fits the definition of a hash function although it is not a practically good one. Writing a
good hash function is not an easy task. You need to consider a number of things about the input data before you can
write a good hash function.
Why would you need a hash code? It is needed for efficient retrieval of data associated with it when the data is
stored in a hash based collection (or container). Before data is stored in a container, its hash code is computed, and
then it is stored at a location (also called a bucket), which is based on its hash code. When you want to retrieve the
data, its hash code is used to find its location in the container, making the retrieval of the information faster. It is worth
noting that an efficient retrieval of data using hash code is based on distribution of the hash code values over a range.
If the hash codes that are generated are not uniformly distributed, the retrieval of data may not be efficient. In the
worst case, the retrieval of data may be as bad as a linear search through all elements stored in the container. If you
use a hash function as shown above, all elements in the container will be stored in the same bucket, which will require
searching through all elements. Using a good hash function so that it gives you uniformly distributed hash codes is
critical in implementing an efficient hash based container for fast data retrieval.
What is the use of hash codes in Java? Java uses hash codes for the same reason described above—to efficiently
retrieve data from hash based collections. If the objects of your class are not used as keys in a hash based collection,
for example, in a Hashtable , HashMap , etc., you need not even worry about hash codes for your objects at all.
You can compute hash code for an object in Java. In case of an object, the pieces of information that will be used
to compute the hash code are the pieces of information that make up the state of the object. Java designers considered
the hash code for an object so important that they provided a default implementation to compute the hash code for
an object in the Object class.
The Object class has a hashCode() method that returns an int , which is the hash code of the object. The default
implementation of this method computes the hash code of an object by converting the memory address of the
object into an integer. Since the hashCode() method is defined in the Object class, it is available in all classes in Java.
However, you are free to override the implementation in your class. Here are the rules that you must follow when you
override the hashCode() method in your class. Suppose there are two object references, x and y .
x.equals(y) returns true , x.hashCode() must return an integer, which is equal to
y.hashCode() . That is, if two objects are equal using the equals() method, they must have the
same hash codes.
If
x.hashCode() is equal to y.hashCode() , it is not necessary that x.equals(y) returns true .
That is, if two objects have the same hash codes using the hashCode() method, they do not
have to be equal using the equals() method.
If
hashCode() method is called on the same object multiple times in the same execution
of a Java application, the method must return the same integer value. The hashCode() and
equals() methods are closely tied. If your class overrides any of these two methods, it must
override both for the objects of your class to work correctly in hash-based collections. Another
rule is that you should use only those instance variables to compute the hash code for an
object, which are also used in the equals() method to check for equality.
If the
 
Search WWH ::




Custom Search