package jhash; public class Hash { public static long getHash(String s, int lenght) { int usedLenght = lenght + 1; long hash = 0; for (int i = 0; i < s.length(); ++i) { hash += s.charAt(i) * (i + 1); } hash = Math.abs(Integer.reverse((int) hash)); while (hash < Math.pow(10, usedLenght - 1) || hash > Math.pow(10, usedLenght)) { if (hash >= Math.pow(10, usedLenght)) { hash = hash / 2; } if (hash < Math.pow(10, usedLenght - 1)) { hash = hash * 2; } } return (hash / 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; } }