Equals Without HashCode

ID

java.equals_no_hashcode

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:581, reliability

Description

Reports classes that override equals(Object) but do not also override hashCode().

Rationale

Object.equals and Object.hashCode are contractually linked: if two instances are equal by equals they must return the same hashCode. Overriding equals while leaving the inherited reference-based hashCode in place breaks this contract — two equal instances will end up in different buckets of a HashMap / HashSet, so lookups silently miss.

Remediation

Override hashCode() consistently with the new equals definition. Any IDE can generate the pair from the same set of fields.