package hashing;
public class Exercise27_09 {
public static void main(String[] args) {
String s = "Welcome";
System.out.println(hashCodeForString(s));
System.out.println(s.hashCode());
}
/** 返回字符串散列码 */
public static int hashCodeForString(String s) {
if (s.length() == 0) return 0;
int hashCode = s.charAt(0);
for (int i = 0; i < s.length() - 1; i++)
hashCode = hashCode * 31 + s.charAt(i + 1);
return hashCode;
}
}