// // Hash a string of arbitrary length. // // This is more robust than the book's code in Fig 5.4 / p. 172 // since it does not overflow the result, and it avoids use of // the magic number '37' (not explained in the text). // public static int hash (String s, int tableSize) { int result = 0; for (int i = 0; i < s.length(); i ++) { // left-shift 8 bits and add in the next character result = 256 * result + s.charAt(i); // invoke Horner's rule: take modulos along the way result = result % tableSize; } return result; }