Created
January 31, 2015 02:02
-
-
Save ZacSweers/a6ac20409f5a28dd675c to your computer and use it in GitHub Desktop.
HashUtil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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