Are you smoking hashCode()?

Here’s a little tip for you. This is a valid use of the Apache Commons HashCodeBuilder:

public int hashCode() {
    return new HashCodeBuilder(17, 37).
        append(name).
        append(age).
        append(smoker).
        toHashCode();
}

This is not:

public int hashCode() {
    return new HashCodeBuilder(17, 37).
        append(name).
        append(age).
        append(smoker).
        hashCode();
}

As you may expect, the second example gives unexpected results.

It's only fair to share...
Share on Facebook
Facebook
Google+
Google+
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Leave a Reply