This class is a convenience method to sequentially calculate hash code of the object based on the field values. The result depends on the order of elements appended. The exact formula is the same as for
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(); }
@see java.util.List#hashCode()