posted 21 years ago
The funtion hashCode relates the value to put in a hashTable with the position of that value in the data estructure. For searching, is quicker to use the hashCode funtion to yield the position instead of searching the whole estructure.
The problem is that the range of values to store is much greater than the capacity of the container. Thus it is not possible to have only one value in each position in the container. Solution: each position in the container (a bucket) is itself another container of a growable capacity, for instance an ArrayList. When the hashCode funtion yields the same bucket for several values, equals is used on the accessed value to compare it with the values placed in the same bucket.
Imagine we want to store Integers ranging from 1 to 20 in an array whose length is 15. We cannot have a hashCode that relates Integer 1 to position 1 etc. Some Integers are going to end up in the same bucket. If the array is of type ArrayList is possible to acomodate several Integers in each position. When an Integer is going to be put into the array, hashCode is invoked on it yielding a position of an ArrayList in which is stored. When the container is questioned for the presence of the Integer, applies the hashCode to get the ArrayList in which it was stored. Now the equals is invoked to compare the requested Integer with all the Integers residing in the ArrayList.
Between hashCode and equals the identity of an object must be determined. The former indexes into the array, and the latter causes a sequential search in a bucket (ArrayList in our example).
For this to be quick --the goal of this structure-- hashcode must spread out the possible values along the array, otherwise some buckets will contain many values; in this situation the sequential search in a bucket ruins the performance.
SCJP2. Please Indent your code using UBB Code