previous | start | next

Hashing Functions for Objects

To use the ideas above, we first need to produce an hashcode: an integer associated with the object to be inserted or searched for.

If the keys are already integers this is not a problem, but what about other types such as String, etc.

But in Java, every class type inherits the hashCode() method from Object.

Does this method need to be overriden? Why or why not?

As noted earlier, the String class does override hashCode().

The hashCode() function gives an integer. But this integer might be negative. So before using it as an index for the open addresssing table or the table of chains, this value must be modified:

        int getIndex(Object x, int tblSize)
        {
           int location = x.hashCode() % tblSize;
           if ( location < 0 )
           {
             location += tableSize;
           }
           return location;
        }
     


previous | start | next