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

Description

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

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.

Remediation

Add an equals(Object) override that matches the hashCode implementation. Most IDEs can generate both together from the same set of fields.