...
Overriding the equals() method without overriding the hashCode() method can lead to unexpected results.
Rule | Severity | Likelihood | Detectable | RepairableRemediation Cost | Priority | Level |
|---|---|---|---|---|---|---|
MET09-J | Low | Unlikely | Yes | NoHigh | P1P2 | L3 |
Automated Detection
Automated detection of classes that override only one of equals() and hashcode() is straightforward. Sound static determination that the implementations of equals() and hashcode() are mutually consistent is not feasible in the general case, although heuristic techniques may be useful.
| Tool | Version | Checker | Description | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| CodeSonar |
| JAVA. CORRECTNESS.HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASSFB.CORRECTNESS.HE_USE_OF_UNHASHABLE_CLASS IDEF.EQUALSNOHC | Defines equals but not hashCode | |||||||
| Klocwork |
| EHC.EQ EHC.HASHSignature declares use of unhashable class in hashed construct Use of class without a hashCode() method in a hashed data structure | ||||||||
| Parasoft Jtest |
| CODSTACERT. | OIMMET09.OVERRIDE | Override 'Object.hashCode()' when you override 'Object.equals()' and vice versa | ||||||
| PVS-Studio |
| V6049 | Implemented||||||||
| SonarQube |
| "equals(Object obj)" and "hashCode()" should be overridden in pairs |
Related Guidelines
...