public final class HashCodeUtil extends Object
hashCode
.
The following utility class allows simple construction of an effective hashCode method.
It is based on the recommendations of Effective Java, by Joshua Bloch.
Example use case:
public int hashCode(){ int result = HashCodeUtil.SEED; //collect the contributions of various fields result = HashCodeUtil.hash(result, fPrimitive); result = HashCodeUtil.hash(result, fObject); result = HashCodeUtil.hash(result, fArray); return result; }
Modifier and Type | Field and Description |
---|---|
static int |
SEED
An initial value for a
hashCode , to which is added contributions
from fields. |
Constructor and Description |
---|
HashCodeUtil() |
Modifier and Type | Method and Description |
---|---|
static int |
hash(int aSeed,
boolean aBoolean)
booleans.
|
static int |
hash(int aSeed,
boolean[] array)
boolean array
|
static int |
hash(int aSeed,
char aChar)
chars.
|
static int |
hash(int aSeed,
double aDouble)
doubles.
|
static int |
hash(int aSeed,
float aFloat)
floats.
|
static int |
hash(int aSeed,
int aInt)
ints.
|
static int |
hash(int aSeed,
long aLong)
longs.
|
static int |
hash(int aSeed,
Object aObject)
aObject is a possibly-null object field, and possibly an array. |
public static final int SEED
hashCode
, to which is added contributions
from fields. Using a non-zero value decreases collisions of hashCode
values.public HashCodeUtil()
public static int hash(int aSeed, boolean aBoolean)
aSeed
- the hash value to append toaBoolean
- the value to hashpublic static int hash(int aSeed, char aChar)
aSeed
- the hash value to append toaChar
- the value to hashpublic static int hash(int aSeed, int aInt)
aSeed
- the hash value to append toaInt
- the value to hashpublic static int hash(int aSeed, long aLong)
aSeed
- the hash value to append toaLong
- the value to hashpublic static int hash(int aSeed, float aFloat)
aSeed
- the hash value to append toaFloat
- the value to hashpublic static int hash(int aSeed, double aDouble)
aSeed
- the hash value to append toaDouble
- the value to hashpublic static int hash(int aSeed, Object aObject)
aObject
is a possibly-null object field, and possibly an array.
If aObject
is an array, then each element may be a primitive
or a possibly-null object.aSeed
- the hash value to append toaObject
- the value to hashpublic static int hash(int aSeed, boolean[] array)
aSeed
- the hash value to append toarray
- the value to hash