Quick revision
The short answer
The method equals(Person other) is an overload, a new method with a different parameter type, not an override of equals(Object). HashSet calls equals(Object), which Person still inherits from Object and which compares identity, so two Person objects with the same id are never equal to it. Their hash codes match, so they land in the same bucket, but the identity check treats them as different and both are kept. Writing @Override would have caused a compile error and exposed the bug; the fix is public boolean equals(Object o) with a type check, or a record.
What to remember
In this answer, we saw that equals(Person) is an overload, not an override, so HashSet, which calls equals(Object), still uses Object's identity comparison. The matching hash codes put both objects in the same bucket, where the identity check called them different, so the set held duplicates. @Override would have turned the mistake into a compile error. The fix is public boolean equals(Object o) with a type check, or a record when equality should cover all fields.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The method equals(Person other) is an overload, a new method with a different parameter type, not an override of equals(Object). HashSet calls equals(Object), which Person still inherits from Object and which compares identity, so two Person objects with the same id are never equal to it. Their hash codes match, so they land in the same bucket, but the identity check treats them as different and both are kept. Writing @Override would have caused a compile error and exposed the bug; the fix is public boolean equals(Object o) with a type check, or a record.
Why this question is asked
This bug is famous because the code looks right at a glance: there is an equals method, there is a hashCode method, and both use the id. Yet the set fills with duplicates. Interviewers use it to check whether we understand the difference between overloading and overriding, how a HashSet decides that two elements are the same, and why @Override exists. In this answer, we will recap overloading versus overriding, trace what the HashSet actually calls, reproduce the duplicates, and fix the class in two ways.
1A Quick Recap of Overloading Versus Overriding
Overriding means a subclass provides its own version of a method with the same name and the same parameter types as a method in its superclass. At runtime, Java picks the version that belongs to the object's actual class. This is how our equals is supposed to replace Object's.
Overloading means declaring another method with the same name but different parameter types. Both methods then exist side by side, and the compiler chooses between them at compile time, based on the declared types of the arguments. Object declares equals(Object). Declaring equals(Person) does not replace it; Person now has two equals methods.
2What HashSet Actually Calls
HashSet stores elements as keys of an internal HashMap. When we add an element, HashMap computes its hashCode, picks a bucket, and for each existing key in that bucket with the same hash, it checks whether the keys are the same object and, if not, calls key.equals(other). The HashMap code was compiled against Object, so the call is equals(Object). The compiler binds it to the method with the parameter type Object, and at runtime Java looks for an override of that exact signature in Person. There is none, so Object.equals runs, and it returns true only if both references point to the same object.
So our equals(Person) is never called by the collection. It is only used when our own code calls equals with an argument whose declared type is Person.
7 messages in order. Play them, or step through them one at a time.
Our equals(Person) is never called; the inherited equals(Object) compares identity.
3Reproducing the Duplicates
Here is the class from the question with a short test.
Notice the two calls to p1.equals. Passing the same object with a different declared type selects a different method. This is what makes the bug confusing: a quick test of p1.equals(p2) in our own code passes, while the collection, which only knows Object, sees the inherited identity check. The matching hash codes are exactly why the objects end up in the same bucket and get compared at all; with Object's default hashCode they would have landed in different buckets and never been compared.
4How @Override Would Have Caught It
The @Override annotation tells the compiler that we intend to override a method of a supertype. If no supertype has a method with that exact signature, compilation fails.
One annotation turns a silent runtime bug into a compile error. That is why many teams require @Override on every overriding method and enable compiler or static analysis checks for it. Error Prone and IDE inspections also warn specifically about equals methods whose parameter is not Object.
5Fix 1: Override equals(Object) Properly
The correct method takes Object, checks the type, and then compares fields. Pattern matching for instanceof makes it short.
Notice that instanceof also handles null, because null instanceof Person is false, so equals(null) returns false instead of throwing. Making id final keeps the hash code stable while the object is in the set, and the class final avoids symmetry problems with subclasses.
6Fix 2: Use a Record
If Person is a simple carrier of data, a record removes the problem entirely, because the compiler generates equals(Object) and hashCode from the components.
If Person has more components but should be equal by id only, a record is not the right fit, since its equals uses all components. Then we write the class as in Fix 1.
7Follow-up questions
Why does Java not reject equals(Person) automatically?
Because overloading is legal and sometimes useful, and the compiler cannot know whether we meant to overload or override. @Override is the way we tell it our intent. Without it, the compiler has no reason to complain.
Would the bug also affect a TreeSet?
No, not in the same way. TreeSet ignores equals and uses compareTo or a Comparator to decide whether two elements are the same. If Person implemented Comparable by id, a TreeSet would reject the duplicate even with the broken equals. Mixing collection types with an inconsistent equals is still a recipe for confusing behaviour.
Is it fine to keep equals(Person) as an extra helper?
It is legal but confusing, because callers get different results depending on the declared type of the argument, as we saw. It is better to have only the equals(Object) override.
8Conclusion
In this answer, we saw that equals(Person) is an overload, not an override, so HashSet, which calls equals(Object), still uses Object's identity comparison. The matching hash codes put both objects in the same bucket, where the identity check called them different, so the set held duplicates. @Override would have turned the mistake into a compile error. The fix is public boolean equals(Object o) with a type check, or a record when equality should cover all fields.
Open with
The problem is that equals(Person other) does not override Object's equals, it overloads it, because the parameter type is different. Person ends up with two equals methods. HashSet stores elements as HashMap keys, and HashMap calls equals with an Object parameter, so the compiler bound that call to equals(Object), and at runtime Person has no override of that signature.
Then, if they want more
The inherited Object.equals runs, which compares identity. The hash codes do match, since both use id.hashCode, so the two objects land in the same bucket and get compared, but the identity check says they are different, so both are kept.
It is confusing because calling p1.equals(p2) directly in our own code returns true, since there the compiler picks the Person overload. Adding @Override would have caught it immediately, because the compiler would report that the method does not override anything.
The fix is to write public boolean equals(Object o), check with instanceof Person, then compare ids, keep hashCode consistent with it, and ideally make id final so the hash cannot change while the object is in the set. If Person is just data, a record is even simpler because it generates correct equals and hashCode.
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.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
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 "A developer writes": https://scrivio-interview-library.netlify.app/java/sets/1ab8170c10/