In my class of data structure we are learning different hash functions, but this one in particular I do not understand why in the last three lines of the code they check if HashVal<0, because since HashVal is the reminder of division for tableSize it should never be less than zero. Please I just want to understand this last part. Thank you in advance.
public static int hash(String key, int tableSize)
{
int hashVal = 0;
for( int i = 0; i < key.length(); i++ )
hashVal = 37 * hashVal + key.charAt(i);
hashVal %= tableSize;
if( hashVal < 0 ) //overflow case
hashVal += tableSize;
return hashVal;
}