Write HashQuality with two hash functions for strings:
sumHash(s)— the sum of the character codes;polyHash(s)— the polynomial hashh = 31·h + cfor each character (whatString.hashCode()computes).
Read the number of buckets m, then words. Put each word in bucket hash mod m for both functions (use Math.floorMod for the polynomial hash, which can be negative) and print, for each function, the bucket sizes, the number of buckets used, the collisions (each word after the first in a bucket) and the longest chain. Finally check that polyHash equals String.hashCode() for the first word.
For the input
7 listen silent enlist tinsel inlets stop pots tops spot opts post
the program prints
11 words, 7 buckets, load factor 1.57
sum of characters: buckets [0 0 0 0 5 0 6]
used 2 of 7, collisions 9, longest chain 6
polynomial (31): buckets [0 1 3 0 2 2 3]
used 5 of 7, collisions 6, longest chain 3
polyHash matches String.hashCode(): true