public final class HashCode extends Object
java.util.List.hashCode
.
If you need order independent hash code just summate, multiply or XOR all
elements.
Suppose we have class:
class Thing {
long id;
String name;
float weight;
}
The hash code calculation can be expressed in 2 forms.
For maximum performance:
public int hashCode() {
int hashCode = HashCode.EMPTY_HASH_CODE;
hashCode = HashCode.combine(hashCode, id);
hashCode = HashCode.combine(hashCode, name);
hashCode = HashCode.combine(hashCode, weight);
return hashCode;
}
For convenience:
public int hashCode() {
return new HashCode().append(id).append(name).append(weight).hashCode();
}
List.hashCode()
Modifier and Type | Field | Description |
---|---|---|
static int |
EMPTY_HASH_CODE |
The hashCode value before any data is appended, equals to 1.
|
Constructor | Description |
---|---|
HashCode() |
Modifier and Type | Method | Description |
---|---|---|
HashCode |
append(boolean value) |
Appends value's hashCode to the current hashCode.
|
HashCode |
append(double value) |
Appends value's hashCode to the current hashCode.
|
HashCode |
append(float value) |
Appends value's hashCode to the current hashCode.
|
HashCode |
append(int value) |
Appends value's hashCode to the current hashCode.
|
HashCode |
append(long value) |
Appends value's hashCode to the current hashCode.
|
HashCode |
append(Object value) |
Appends value's hashCode to the current hashCode.
|
static int |
combine(int hashCode,
boolean value) |
Combines hashCode of previous elements sequence and value's hashCode.
|
static int |
combine(int hashCode,
double value) |
Combines hashCode of previous elements sequence and value's hashCode.
|
static int |
combine(int hashCode,
float value) |
Combines hashCode of previous elements sequence and value's hashCode.
|
static int |
combine(int hashCode,
int value) |
Combines hashCode of previous elements sequence and value's hashCode.
|
static int |
combine(int hashCode,
long value) |
Combines hashCode of previous elements sequence and value's hashCode.
|
static int |
combine(int hashCode,
Object value) |
Combines hashCode of previous elements sequence and value's hashCode.
|
int |
hashCode() |
Returns accumulated hashCode
|
public static final int EMPTY_HASH_CODE
List.hashCode()
,
Constant Field Valuespublic final int hashCode()
public static int combine(int hashCode, boolean value)
hashCode
- previous hashCode valuevalue
- new elementpublic static int combine(int hashCode, long value)
hashCode
- previous hashCode valuevalue
- new elementpublic static int combine(int hashCode, float value)
hashCode
- previous hashCode valuevalue
- new elementpublic static int combine(int hashCode, double value)
hashCode
- previous hashCode valuevalue
- new elementpublic static int combine(int hashCode, Object value)
hashCode
- previous hashCode valuevalue
- new elementpublic static int combine(int hashCode, int value)
hashCode
- previous hashCode valuevalue
- new elementpublic final HashCode append(int value)
value
- new elementpublic final HashCode append(long value)
value
- new elementpublic final HashCode append(float value)
value
- new elementpublic final HashCode append(double value)
value
- new elementpublic final HashCode append(boolean value)
value
- new elementCopyright © 2018. All rights reserved.