I have recently discovered the Objects.hash() method.
My first thought was, that this tidies up your hashCode() implementation a lot. See the following example:
@Override
//traditional
public int hashCode() {
int hash = 5;
hash = 67 * hash + (int)(this.id ^ (this.id >>> 32));
hash = 67 * hash + (int)(this.timestamp ^ (this.timestamp >>> 32));
hash = 67 * hash + Objects.hashCode(this.severity);
hash = 67 * hash + Objects.hashCode(this.thread);
hash = 67 * hash + Objects.hashCode(this.classPath);
hash = 67 * hash + Objects.hashCode(this.message);
return hash;
}
@Override
//lazy
public int hashCode() {
return Objects.hash(id, timestamp, severity, thread, classPath, message);
}
Although I have to say that this seems too good to be true. Also I've never seen this usage.
Are there any downsides of using Objects.hash() compared to implementing your own hash code? When would I choose each of those approaches?
Update
Although this topic is marked as resolved, feel free to keep posting answers that provide new information and concerns.
HashCodeBulider: commons.apache.org/proper/commons-lang/apidocs/org/apache/…@EqualsAndHashCode.