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 FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply