Quick revision
The short answer
Version (a), return 1, is legal but puts every Point into one bucket, so the map degrades into a slow search even with Java 8's tree bins. Version (b), x + y, collides for symmetric points such as (1,2) and (2,1) and for every point on the same diagonal, so a grid of points piles into a few buckets. Version (c), Objects.hash(x, y), is correct and spreads well, but it allocates a varargs array and boxes both ints on each call; 31 * x + y, or simply declaring Point as a record, gives the same quality without that cost.
What to remember
In this answer, we saw that return 1 is legal but puts every key in one bucket, making the map slow even with tree bins. x + y collides for symmetric points and whole diagonals, leaving a grid of points in a few hundred crowded buckets. Objects.hash is correct and spreads well but allocates a varargs array and boxes the ints on each call. 31 * x + y avoids the allocation, a record Point(int x, int y) generates a good implementation for us, and in every case hashCode must use only fields that equals uses.
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.
Version (a), return 1, is legal but puts every Point into one bucket, so the map degrades into a slow search even with Java 8's tree bins. Version (b), x + y, collides for symmetric points such as (1,2) and (2,1) and for every point on the same diagonal, so a grid of points piles into a few buckets. Version (c), Objects.hash(x, y), is correct and spreads well, but it allocates a varargs array and boxes both ints on each call; 31 * x + y, or simply declaring Point as a record, gives the same quality without that cost.
Why this question is asked
Every HashMap lookup starts with the key's hashCode, so a poor hashCode quietly turns a fast map into a slow one, and nothing fails: the results are still correct. That is why these bugs reach production. Interviewers ask this review question to see whether we can tell legal from good, explain the effect of collisions on HashMap, and know the modern idiom for writing equals and hashCode. In this answer, we will recap what HashMap needs from hashCode, review each version with numbers, look at the recommended implementation, and finish with the one rule every formula must follow.
1A Quick Recap of What HashMap Needs From hashCode
HashMap uses the key's hashCode to pick a bucket, and then equals to find the exact key inside that bucket. The contract has one hard rule: equal objects must have equal hash codes. Unequal objects may share a hash code, which is called a collision, and that is legal.
Legal is not the same as good, though. HashMap is fast because each bucket holds only a handful of entries. If many keys share a hash code, they share a bucket, and every lookup in that bucket must compare against many keys. A good hashCode spreads different keys across many different values, so that buckets stay small.
2Version (a): return 1
This satisfies the contract, because equal points get equal hash codes. But every point gets the same hash code, so every entry goes into the same bucket.
Since Java 8, a bucket with 8 or more entries in a table of at least 64 buckets becomes a red-black tree, which limits the damage. But the tree orders entries by hash first, and here every hash is equal. If Point implements Comparable, the tree can fall back to compareTo and lookups take about log2(900), roughly 10 comparisons, and the cost keeps growing with the map. If it does not, the tree has no way to decide left or right and must search both sides, so lookups approach a linear scan with an equals call per entry. Either way, the map does far more work than the one or two comparisons a healthy map needs, and every insert also pays that cost.
3Version (b): return x + y
This looks reasonable, but addition is symmetric and loses information. (1,2) and (2,1) both give 3. Worse, every point on the same diagonal, where x + y is constant, collides: (0,10), (1,9), (2,8) and so on all give 10.
| Implementation | Distinct hashes | Typical bucket |
|---|---|---|
| return 1 | 1 | 900 entries in one bucket |
| return x + y | 59 | up to 30 entries |
Objects.hash(x, y) | 900 | one or two entries |
| 31 * x + y | 900 | one or two entries |
So for a grid, which is exactly the kind of data Points tend to represent, the map has 59 crowded buckets instead of 900 nearly empty ones. Lookups are much slower than they should be, and the problem grows with the size of the grid.
4Version (c): Objects.hash(x, y)
Objects.hash is correct and spreads well. It computes 31 * (31 * 1 + x) + y, which gives different values for (1,2) and (2,1) because the first field is multiplied before the second is added. Multiplying by 31, an odd prime, mixes the fields so that small changes in either one change the result.
The cost is in how it is called. Objects.hash takes Object... values, so each call creates an Object[] array and boxes both ints into Integer objects. For a key used in a hot loop, that is three small allocations per hashCode call. The JIT can sometimes remove them through escape analysis, but we should not rely on it in a performance-sensitive key class.
5The Recommended Implementation
We get the same quality without allocation by writing the formula directly, and even better, by letting the compiler write it.
A record generates equals and hashCode from all its components, and the generated hashCode combines them well without allocating. It also makes the fields final, which keeps the hash stable while the point sits in a map. For a value key like Point, a record is the best answer.
One honest caveat: 31 * x + y is a reasonable spread, not a perfect one. Once y reaches 31, pairs such as (0,31) and (1,0) share a value, so a very large dense grid still sees some collisions. They are spread thinly across thousands of values instead of piling up on diagonals, and HashMap's bins stay small. If profiling ever shows otherwise, a larger odd multiplier spreads wide coordinates better.
6The Rule Every Formula Must Follow
Whatever formula we choose, hashCode must use only fields that equals also uses. If hashCode used a field that equals ignores, such as a cached label, two equal points could have different hash codes, land in different buckets, and a lookup with an equal key would miss. Using fewer fields than equals is legal but weakens the spread, which is how version (a) and a hash of only x would go wrong. The safest path is to derive both methods from the same list of fields, which is what records and IDE generators do.
7Follow-up questions
Why is 31 used as the multiplier?
It is an odd prime, so multiplication by it does not lose the lowest bit, and it spreads values reasonably. It can also be computed as (i << 5) - i, which was a cheap operation on older hardware. String.hashCode uses it too, and it is a convention more than a magic number; any odd multiplier of reasonable size works.
Does HashMap's own hash mixing fix a bad hashCode?
No. HashMap XORs the high 16 bits into the low 16 bits to spread hash codes that differ only in high bits. It cannot create differences that do not exist. If two keys return the same hashCode, they always land in the same bucket.
Would making Point Comparable help version (a)?
It helps the tree bins, because a treeified bucket can then order entries by compareTo and search in O(log n) instead of scanning. But it only reduces the damage. The fix is a hashCode that spreads keys, so buckets stay small and treeification never happens.
8Conclusion
In this answer, we saw that return 1 is legal but puts every key in one bucket, making the map slow even with tree bins. x + y collides for symmetric points and whole diagonals, leaving a grid of points in a few hundred crowded buckets. Objects.hash is correct and spreads well but allocates a varargs array and boxes the ints on each call. 31 * x + y avoids the allocation, a record Point(int x, int y) generates a good implementation for us, and in every case hashCode must use only fields that equals uses.
Open with
Version a, return 1, is legal because equal points still get equal hash codes, but every key lands in the same bucket. Since Java 8 that bucket becomes a tree, but all hashes are equal, so unless Point is Comparable, lookups degrade toward a linear scan with equals calls, and even with Comparable it is many comparisons instead of one or two.
Then, if they want more
Version b, x plus y, is better but collides for symmetric points like 1,2 and 2,1, and for every point on the same diagonal. A 30 by 30 grid has 900 points but only 59 distinct sums, so buckets hold up to 30 entries.
Version c, Objects.hash of x and y, is correct and spreads well, because it multiplies by 31 before adding the next field. Its only cost is that it allocates a varargs array and boxes both ints on every call, which matters for a key in a hot path.
I would write 31 times x plus y directly, or better, declare Point as a record, which generates equals and hashCode from the same fields with good spreading and makes them final. And whatever formula is used, hashCode must only use fields that equals uses.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- 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 "Review these hashCode implementations for a Point(int x, int y) used as a HashMap key...": https://scrivio-interview-library.netlify.app/java/hashmap-internals/1a18618bc4/