Skip to content

Instantly share code, notes, and snippets.

@ZacSweers
Created January 31, 2015 02:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZacSweers/a6ac20409f5a28dd675c to your computer and use it in GitHub Desktop.
Save ZacSweers/a6ac20409f5a28dd675c to your computer and use it in GitHub Desktop.
HashUtil
/**
* Utility functions for hashing sets of values, based on the methods described in Ch. 3 of Effective Java
*/
public class HashUtil {
public static int hash(boolean... args) {
int result = 0;
for (boolean arg : args) {
result += 31 * result + (arg ? 1 : 0);
}
return result;
}
public static int hash(byte... args) {
int result = 0;
for (byte arg : args) {
result += 31 * result + (int) arg;
}
return result;
}
public static int hash(char... args) {
int result = 0;
for (char arg : args) {
result += 31 * result + (int) arg;
}
return result;
}
public static int hash(short... args) {
int result = 0;
for (short arg : args) {
result += 31 * result + (int) arg;
}
return result;
}
public static int hash(int... args) {
int result = 0;
for (int arg : args) {
result += 31 * result + arg;
}
return result;
}
public static int hash(long... args) {
int result = 0;
for (long arg : args) {
result += 31 * result + (int) (arg ^ (arg >>> 32));
}
return result;
}
public static int hash(float... args) {
int result = 0;
for (float arg : args) {
result += 31 * result + Float.floatToIntBits(arg);
}
return result;
}
public static int hash(double... args) {
int result = 0;
for (double arg : args) {
long l = Double.doubleToLongBits(arg);
result += 31 * result + (int) (l ^ (l >>> 32));
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment