in java PMD rules, there is a rule OverrideBothEqualsAndHashcode.
it means that developer has to override both equals(Object obj) and hashCode() not just one.
can someone explain why?
and if i override and re-define eqauls(Object obj), what should i implement in hashCode() ?
class MyClass() {
public int id;
@Override
public boolean equals(Object obj) {
return id == ((MyClass) obj).id;
}
@Override
public int hashCode() {
// WHAT KIND OF CODE SHOULD I IMPLEMENT HERE?
}
}