I am creating my own hashtable adt for my data structures class and have run across a problem. I used the following functions to hash (key,value) entries in my hash table(the key is a string and the value can be any data type,it is generic):
private int hashCode(String key)
{
final int constant = 37;
int hash = 0;
for(int i=0;i<key.length();i++)
{
hash+=(int)key.charAt(i) * Math.pow(constant,i);
}
return hash;
}
private int hash(String key)
{
return hashCode(key) % capacity
}
Using linear probing the inserts into the table work fine,however if I ever choose to expand the table the hash(key) function will not work adequately for the get(key) operation of a hash table as the capacity has changed(it will map to the incorrect location when before the table extension it mapped correctly). Is there any easy way I could edit this which takes into account a extension of a hash table by lets say a factor of two. Ie: if loadfactor > 0.5 increase table by 2* the capacity.