Skip to content

equals-hash-code

Javacorrectnessno fixdefault: warn

Reports a class that declares boolean equals(Object) and does not declare hashCode().

Objects that are equal must have the same hash code. Without an own hashCode the one of Object is used, so two equal objects land in different buckets and a HashMap or HashSet does not find them.

To fix, declare hashCode() from the same fields as equals.

Reported
class A {
public boolean equals(Object o) {
...
}
...
java:
lint:
rules:
equals-hash-code: warn # off, warn (default), error

Suppress one line with sudsy-disable-next-line equals-hash-code in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
checkstyle EqualsHashCode partial Only equals without hashCode, not the reverse.