Java examples for java.lang:String Hash
Calculates a low collision hash code for the passed string
//package com.java2s; import java.util.Arrays; public class Main { public static void main(String[] argv) { String s = "java2s.com"; System.out.println(longHashCode(s)); }/*w w w. ja va 2 s . com*/ /** * Calculates a low collision hash code for the passed string * @param s The string to calculate the hash code for * @return the long hashcode */ public static long longHashCode(String s) { long h = 0; int len = s.length(); int off = 0; int hashPrime = s.hashCode(); char val[] = s.toCharArray(); for (int i = 0; i < len; i++) { h = (31 * h + val[off++] + (hashPrime * h)); } return h; } /** * Calculates a low collision hash code for the passed byte array * @param arr The byte array to calculate the hash code for * @return the long hashcode */ public static long longHashCode(byte[] arr) { long h = 0; int len = arr.length; int off = 0; int hashPrime = Arrays.hashCode(arr); for (int i = 0; i < len; i++) { h = (31 * h + arr[off++] + (hashPrime * h)); } return h; } }