I want to "implement" a hash function from Strings to shorts, using the java standard hashCode() function of String object. I came up with the following simple implementation:
static short shortHashCode(String str)
{
int strHashCode = str.hashCode();
short shorterHashCode = (short) (strHashCode % Short.MAX_VALUE);
return shorterHashCode;
}
- Is my
shortHashCodefunction a good hash function? Meaning is the chance of collisions small (chance that two different Strings will have the same hash code close to 1/Short.MAX_VALUE) ? - Is there a better way to implement hash function from Strings to shorts?