HashCode Without Equals
ID |
java.hashcode_no_equals |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:581, reliability |
Rationale
Object.hashCode and Object.equals are contractually linked: two objects that are equal by equals must return the same hashCode. Overriding hashCode alone implies a custom notion of equality that is never expressed, so the inherited Object.equals (reference equality) remains in effect — instances look unequal to each other yet share a hash, breaking HashMap / HashSet lookups for that type.
Either override both methods with a consistent definition, or override neither.