package jhash; public class Hash { public static long getHash(String s, int lenght) { int usedLenght = lenght + 1; long tmp = 0; for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); int j = (int) c; tmp += (j*(i+1)); } tmp = Math.abs(Integer.reverse((int)tmp)); while (tmp < Math.pow(10, usedLenght - 1) || tmp > Math.pow(10, usedLenght)) { if (tmp >= Math.pow(10, usedLenght)) { int zbytek = (int)(tmp % 2); tmp = ((tmp / 2) + zbytek) - (zbytek / 2); } if (tmp < Math.pow(10, usedLenght - 1)) { tmp = tmp * 2; } } return (tmp / 10); } public static int hashCode(String s) { int hash = 0; int lenght = s.length(); for(int i =0; i < lenght; i++) { hash += s.charAt(i) * (int)Math.pow(31, lenght - (i + 1)); } return hash; } }