How do we decide on the best implementation of hashCode()
method for a collection (assuming that equals method has been overridden correctly) ?
If you're happy with the Effective Java implementation recommended by dmeister, you can use a library call instead of rolling your own:
@Override
public int hashCode() {
return Objects.hashCode(this.firstName, this.lastName);
}
This requires either Guava (com.google.common.base.Objects.hashCode
) or the standard library in Java 7 (java.util.Objects.hash
) but works the same way.