Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Bugs from broken equals and hashCode have appeared in your organization for years: mutable keys, missing hashCode, entity equality mistakes. What would you standardise or forbid, and how do you know it worked?

I would standardise value types on records or generated implementations and write down a single rule for entity equality; forbid hand-written equals without tests, mutable fields in hash keys and equals overloads that take a non-Object parameter; ship EqualsVerifier in the shared test starter; and turn on static analysis rules for missing hashCode, equals without @Override and reference comparison of objects. To know it worked, I would tag equality-related incidents and defects, compare their counts before and after, and watch leading indicators such as rule violations falling.

Why this question is asked

When the same class of bug keeps coming back for years, the problem is no longer individual knowledge; it is that the organisation makes the wrong thing easy and the right thing optional. Equality bugs are a good example because they are cheap to prevent and expensive to find: a missing hashCode shows up as a cache that never hits, a mutable key as a slow memory leak. Interviewers ask this leadership question to see whether we can turn a recurring technical problem into rules, tools and measurement, without drowning teams in process. In this answer, we will first look at the bugs as one problem, then go through what to standardise, what to forbid, how tooling enforces it, how to roll it out, and how to prove it worked.

1Seeing the Bugs as One Problem

The first step is to collect the past incidents and classify them, because the remedy depends on the cause. Suppose a review of two years of postmortems and bug tickets finds 23 equality-related defects: 7 mutable keys in hash maps or sets, 5 classes with equals but no hashCode, 6 JPA entity equality mistakes, 3 equals methods that overloaded instead of overriding, and 2 comparisons of objects with ==.

Notice what these have in common. In almost every case, the code was written by hand, the mistake was detectable by a tool, and no test exercised the contract. That tells us the leverage is in defaults and automation, not in more training slides.

2What to Standardise

For value types, the standard is records, or IDE- or Lombok-generated implementations over all fields on final, immutable classes when a record does not fit. This removes most hand-written equals at a stroke, and with it most missing-hashCode and field-mismatch bugs, because generated methods cannot drift apart.

For entities, we write one documented rule and implement it once: equality by an immutable business key where one exists, otherwise by non-null ID with a constant hashCode per class, using instanceof so framework proxies match, and never including associations. The rule lives in a short page with an example and, if teams want it, a shared base class or template. The key word is single: six entity bugs came from six different ideas of what entity equality means.

3What to Forbid

Three things should be explicitly forbidden, and each earns its place because it caused real incidents. The first is a hand-written equals without a test, because the five missing-hashCode bugs all lived in methods nobody had ever checked; if a person writes equals, a test must check it. The second is a mutable field in a hash key, because a type used as a HashMap key or a HashSet element that changes a field used by equals or hashCode is exactly how the seven lost-key bugs happened. The third is an equals overload such as equals(Order other), which is the most deceptive of all: it looks like an override, compiles without complaint, and is never called by collections, which only know equals(Object).

Java
// Forbidden: overload, never used by HashMap or HashSet
public boolean equals(Order other) { return id == other.id; }
// Required: override with @Override, so the compiler checks the signature
@Override public boolean equals(Object o) { return o instanceof Order other && id == other.id; }

Forbidding things only works when the rule is checkable, so each item on this list is paired with a tool in the next section. A rule nobody can check becomes a suggestion.

4Making It Automatic: Tests and Static Analysis

We add EqualsVerifier to the shared test starter, the dependency every service already uses for tests, so a contract test is one line and needs no extra setup. Our standard then says: every hand-written equals has a test like EqualsVerifier.forClass(Money.class).verify().

Static analysis catches the rest at compile time or in CI. Error Prone has checks such as EqualsHashCode, MissingOverride, NonOverridingEquals and ReferenceEquality; SpotBugs has patterns such as HE_EQUALS_NO_HASHCODE and EQ_SELF_NO_OBJECT; Sonar has equivalent rules (check the exact rule names for your tool versions). We enable them as errors in the shared build configuration. An architecture test, for example with ArchUnit, can also check that classes annotated or named as value types are records or final.

Step throughLayers that stop equality bugs
  1. Standardrecords for values, one entity rule
  2. Generationrecords and generated methods remove hand-written code
  3. Compile and CI checksmissing hashCode, overloads, == on objects
  4. Contract testsEqualsVerifier for every hand-written equals
  5. Review and incident tagginghumans catch the rest; data shows what slips through

5 layers, from the top down. Step through them one at a time.

Each layer catches what the one above lets through.

5Rolling It Out Without Stopping Everyone

Turning on strict rules across hundreds of repositories at once would produce thousands of failures and a lot of resentment. A ratchet works better: record the existing violations as a baseline, fail the build only for new ones, and let teams pay down the baseline over time, starting with code that handles keys of caches and sets.

I would pilot with two or three teams that had equality incidents, fix the false positives in the rule set with them, and then make it the default in the shared build template. Teams adopt rules faster when the first people to use them are respected engineers who say the rules found real bugs.

6Knowing It Worked

The outcome measure is the count of defects and incidents caused by equality bugs, before and after. For that, the bug tracker and postmortem template need a root-cause tag, such as equality-contract, applied consistently. If the baseline was 23 in two years, roughly one a month, we would expect a clear drop within two or three quarters, and we report the number, not an impression.

Because incidents are rare and noisy, I would also track leading indicators: the number of static analysis violations in the baseline going down, the share of value types that are records, the number of hand-written equals methods without a test, and the number of new violations blocked in CI, each of which is a bug that did not ship. If incidents still occur, each one is reviewed to find which layer should have caught it, and that layer is improved.

7Follow-up questions

How do you convince teams that this is worth their time?

Show them their own data: the incidents, how long each took to diagnose and what it cost. Then show that the fix costs them almost nothing, records and a one-line test, and that the build does the checking. Rules that reduce work, rather than add review steps, meet little resistance.

What if a team has a good reason to break a rule?

The tooling should allow a documented, reviewed suppression, such as an annotation with a reason, rather than forcing workarounds. Suppressions are counted and reviewed periodically; if one rule is suppressed often, the rule probably needs refining.

Would you ban Lombok?

Not entirely. Lombok's generated equals is fine for simple final value classes, though records now cover most of them. I would forbid @Data and @EqualsAndHashCode on JPA entities, where including every field is wrong, and enforce that with an architecture test.

8Conclusion

In this answer, we treated years of equality bugs as one organisational problem. We standardised on records or generated methods for value types and a single rule for entities, forbade hand-written equals without tests, mutable hash keys and equals overloads, and backed each rule with tooling: EqualsVerifier in the shared test starter, static analysis in the shared build and architecture tests. We rolled it out with a baseline and a ratchet, and measured success with tagged incident counts and leading indicators.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would look at the history as data. If I classify the past incidents, they usually fall into a few buckets, mutable keys, missing hashCode, entity equality mistakes, equals overloads and reference comparisons, and almost all of them were hand-written code that a tool could have flagged.

Then, if they want more

So I would standardise records, or generated equals over all fields on final immutable classes, for every value type, and write one documented rule for entity equality: a business key, or a non-null ID with a constant hashCode, never associations.

I would forbid three things: hand-written equals without a test, mutable fields in anything used as a hash key, and equals overloads with a non-Object parameter.

Then I would make it automatic: EqualsVerifier in the shared test starter so a contract test is one line, and static analysis rules for missing hashCode, missing @Override and == on objects, enabled as errors in the shared build. I would roll it out with a baseline and a ratchet so only new violations fail.

To know it worked, I would tag equality root causes in incidents and bugs, compare the counts before and after, and track leading indicators like violations blocked in CI.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Bugs from broken equals and hashCode have appeared in your organization for years...": https://scrivio-interview-library.netlify.app/java/object-contracts/af5543b82c/