Quick revision
The short answer
It almost certainly prints null. Point overrides equals() but not hashCode(), so the two equal points keep the identity hash codes they inherit from Object, which differ. HashMap looks in the bucket chosen by the second point's hash, and even if that happens to be the same bucket, it compares stored hashes before calling equals(), so it never finds the first point. We fix it by overriding hashCode() with the same fields, for example Objects.hash(x, y), or by declaring Point as a record.
What to remember
In this answer, we saw that the code prints null because Point overrides equals() without hashCode(). The two equal points carry different identity hash codes, so HashMap usually looks in a different bucket, and even in the same bucket it rejects the node on the stored hash before equals() is called. The equals and hashCode contract requires equal objects to have equal hashes, and we satisfy it with Objects.hash(x, y), a hand-written 31 * x + y, or, best of all for a value class, a record that generates both methods.
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.
It almost certainly prints null. Point overrides equals() but not hashCode(), so the two equal points keep the identity hash codes they inherit from Object, which differ. HashMap looks in the bucket chosen by the second point's hash, and even if that happens to be the same bucket, it compares stored hashes before calling equals(), so it never finds the first point. We fix it by overriding hashCode() with the same fields, for example Objects.hash(x, y), or by declaring Point as a record.
Why this question is asked
This is the classic equals and hashCode bug, and it still appears in real code reviews, especially in small value classes written quickly for a map key. Interviewers ask it because the answer shows whether we know how HashMap finds a key and what the contract between equals() and hashCode() actually requires. In this answer, we will recap the two methods and their contract, run the code and see why it prints null, follow the lookup step by step, and then fix it three ways, ending with a record.
1A Quick Recap of equals() and hashCode()
Every Java object inherits two methods from Object. equals() answers whether two objects mean the same thing; by default it is identity, so two separate objects are never equal. hashCode() returns an int that hash-based collections use to decide where to store an object; by default it is the identity hash code, a number the JVM assigns to each object, which is effectively unrelated to the object's fields.
The contract in Object's Javadoc ties them together: if a.equals(b) is true, then a.hashCode() must equal b.hashCode(). The reverse is not required, since different objects may share a hash. When we override equals() to compare fields, we must override hashCode() to use the same fields, or we break that rule.
2Running the Code
Here is the class from the question, with the two hash codes printed.
Notice that equals() says the points are equal, but their hash codes are unrelated numbers. That is the broken contract, and it is enough to make get() fail.
3Following the Lookup Step by Step
When we call put(), HashMap calls hashCode() on the key, spreads the bits with h ^ (h >>> 16), and picks a bucket with (n - 1) & hash, where n is the table length, 16 for a new map. It stores a node holding the hash, the key and the value in that bucket.
When we call get(probe), HashMap does the same with the probe's hash. Because the probe's identity hash differs, the bucket index is usually different, and that bucket is empty, so get() returns null without ever calling equals().
Sometimes the two different hashes happen to give the same bucket; with 16 buckets that is roughly a one in sixteen chance. Even then, HashMap checks each node with node.hash == hash before it calls equals(). The stored hash and the probe's hash differ, so the node is skipped and equals() is still never called. That is why the answer is null almost every time, not just most of the time.
- Call probe.hashCode()identity hash, unrelated to x and y
- Pick bucket (n - 1) & spread(hash)
- Bucket empty?usual caseYesReturn nullNoCheck the nodes
- node.hash == hash?stored hash came from a different objectYesCall equals()NoSkip the node, return null
4 steps in order. Play them, or step through them one at a time.
With a missing hashCode(), equals() is never reached.
4Fix 1: Override hashCode() With Objects.hash
The simplest fix is to compute the hash from the same fields equals() compares.
Objects.hash is short and clear. It boxes the ints into a varargs array, which costs a small allocation per call, so in very hot code we can write the formula by hand.
5Fix 2: Write the Formula by Hand
The multiplier 31 is a small odd prime that mixes the fields so that (1, 2) and (2, 1) get different hashes; a plain x + y would give both the same value. Either version satisfies the contract, because equal points always produce the same number.
6Fix 3: Declare Point as a Record
Since Java 16, the cleanest answer for a small value class is a record. The compiler generates the constructor, the accessors, equals(), hashCode() and toString(), all based on the components.
The record's fields are final, so the point cannot change after it is used as a key, which removes a second class of HashMap bugs. And because equals() and hashCode() are generated from the same component list, they can never drift apart when someone adds a field later.
7Follow-up questions
Could it ever print a by chance?
In theory, only if the two Point objects got the same identity hash code, because HashMap compares the stored hash before calling equals(). Identity hashes are generated by the JVM, on HotSpot by default from a thread-local pseudo-random generator, so two objects can share one, but the chance is tiny, roughly one in a few billion. Landing in the same bucket is not enough. For practical purposes it prints null every time, and a test that passes by luck would be worse than one that fails.
Why does a record solve this automatically?
A record's equals() and hashCode() are generated from its components. equals() compares each component, and hashCode() combines the hash of each component, so equal records always have equal hashes and the contract holds by construction. The components are also final, so the hash cannot change while the record sits in a map.
What about the opposite mistake, overriding hashCode() but not equals()?
Then two equal-looking points hash to the same bucket, but equals() is still identity, so HashMap treats them as different keys. get() with a new Point returns null, and putting two such points stores two entries. Both methods must be overridden together.
8Conclusion
In this answer, we saw that the code prints null because Point overrides equals() without hashCode(). The two equal points carry different identity hash codes, so HashMap usually looks in a different bucket, and even in the same bucket it rejects the node on the stored hash before equals() is called. The equals and hashCode contract requires equal objects to have equal hashes, and we satisfy it with Objects.hash(x, y), a hand-written 31 * x + y, or, best of all for a value class, a record that generates both methods.
Open with
It will almost certainly print null. Point overrides equals but not hashCode, so it inherits Object's identity hash code, and the two Point(1, 2) objects have different hashes even though equals says they are the same.
Then, if they want more
That breaks the contract that equal objects must have equal hash codes. When get runs, HashMap spreads the probe's hash and picks a bucket with n minus one AND hash, and that is usually a different bucket from the one where the first point was stored, so it returns null without calling equals at all.
Even if both land in the same bucket, HashMap compares the stored hash in the node before calling equals, and those hashes differ, so the node is skipped. To fix it, I override hashCode using the same fields as equals, either Objects.hash(x, y) or 31 times x plus y if I want to avoid boxing in a hot path.
My preferred fix for a small value type like this is to make it a record, record Point(int x, int y), because the compiler generates equals and hashCode from the same components, so they cannot drift apart, and the fields are final, so the key cannot change while it is in the map.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- 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
- java.util.HashMap, API documentation, The Collections Framework, overview
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/hashmap-internals/aa886317aa/