Quick revision
The short answer
Frames under HashMap$TreeNode.find mean some bins have been converted into red-black trees, which only happens when many keys share a hash or a bucket, so I suspect OrderKey.hashCode() uses too few fields or a low-cardinality field, and that OrderKey is not Comparable, so the tree cannot use compareTo and keeps calling equals(). I would confirm it by sampling keys and counting distinct hash codes and bucket sizes, fix hashCode() to combine every field that equals() uses, implement Comparable consistently with equals if it fits, and benchmark before and after with JMH or a production-like load.
What to remember
In this answer, we saw that HashMap$TreeNode.find frames mean bins have been treeified, so many OrderKey instances share a hash or a bucket. The likely cause is a hashCode() that uses too few fields or a low-cardinality field, made worse when OrderKey is not Comparable, because the tree then cannot use compareTo and falls back to calling equals() across subtrees. We confirmed it by counting distinct hash codes and bucket sizes on real keys, fixed hashCode() to combine all fields used by equals(), added a Comparable consistent with equals, and verified the result with JMH and a production-like load test.
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.
Frames under HashMap$TreeNode.find mean some bins have been converted into red-black trees, which only happens when many keys share a hash or a bucket, so I suspect OrderKey.hashCode() uses too few fields or a low-cardinality field, and that OrderKey is not Comparable, so the tree cannot use compareTo and keeps calling equals(). I would confirm it by sampling keys and counting distinct hash codes and bucket sizes, fix hashCode() to combine every field that equals() uses, implement Comparable consistently with equals if it fits, and benchmark before and after with JMH or a production-like load.
Why this question is asked
A HashMap should spend almost no time in equals(). When a flame graph shows the opposite, the map is doing far more comparisons per lookup than it should, and the cause is nearly always the key class rather than the map. Interviewers like this question because it rewards reading a profile carefully: the frame names point straight at the internal state of the map. In this answer, we will recap what a flame graph shows and what tree bins are, read the frames to form a hypothesis, reproduce the problem, confirm it with measurements, fix the key class, and verify the fix with a benchmark.
1A Quick Recap: Reading the Profile
A flame graph is a picture of sampled stack traces. Each box is a method, the width of a box is the share of samples in which that method was on the stack, and boxes stacked above it are the methods it called. A wide box for HashMap.getNode with a wide equals() box on top tells us that lookups are slow because they call equals() many times.
The frame HashMap$TreeNode.find adds a precise clue. A HashMap bin holds a linked list of nodes until it reaches 8 nodes in a table of at least 64 slots, and then it is converted into a red-black tree of TreeNode objects. TreeNode.find is the method that searches such a tree. So its presence means that at least some bins hold many keys. With 200,000 entries and the default load factor, the table has grown to 524,288 slots (it doubles once the size passes 196,608), and a healthy map would have almost no bin over two or three nodes.
2Forming the Hypothesis
Two properties of OrderKey would produce this picture. The first is a weak hashCode(), one that leaves out fields equals() compares or relies on a field with few distinct values, such as a status, a region code or a date. Many different keys then share a hash, and they crowd into the same bins.
The second is that OrderKey is not Comparable. Inside a tree bin, keys with different hashes are ordered by hash, but keys with equal hashes need compareTo to decide left or right. Without it, TreeNode.find cannot tell which subtree to search, so it searches the right subtree recursively and then the left, calling equals() on the way. With equal hashes and no Comparable, a tree bin can be almost as slow as a list, which matches a profile dominated by equals().
3Reproducing It
Here is a key class of the kind we suspect, and the measurement that exposes it.
Five distinct hash codes for 200,000 keys means five enormous tree bins of about 40,000 nodes each, all with equal hashes inside, and no Comparable to guide the search. Every lookup walks a large part of one tree.
4Confirming It in the Real Service
We should confirm with the real keys rather than assume. The quickest check is the distinct-hash count above, run in a unit test against a sample of production keys or in a debug endpoint: the ratio of distinct hashes to keys should be close to 1. A second check computes the bucket each key would use and looks at the largest bucket.
- Wide getNode and equals() frameslookups compare far too many keys
- TreeNode.find present?YesBins have treeified: many keys per binNoLong list bins or a slow equals()
- Count distinct hashCode valueson a sample of real keys
- Distinct hashes far below key count?YesFix hashCode()NoCheck equals() cost and Comparable
- Benchmark before and afterJMH or a load test
5 steps in order. Play them, or step through them one at a time.
After the fix we expect close to 200,000 distinct hashes and a largest bucket of only a handful of nodes. Writing this as a unit test that fails when the largest bucket exceeds a small limit keeps the problem from returning. Allocation and CPU profiles taken with async-profiler or JFR before and after give the production-side evidence.
5Fixing the Key Class
The fix is to make hashCode() use every field that equals() uses, so that different keys almost always get different hashes. A record does this for us and also gives us immutability. If the keys have a natural order, implementing Comparable consistently with equals, meaning compareTo returns 0 exactly when equals returns true, gives tree bins a fast path for any remaining equal-hash collisions.
Notice that compareTo uses the same three fields as equals(), so it is consistent with equals. An inconsistent compareTo would be worse than none, because the tree would treat unequal keys as the same place to look.
6Verifying With a Benchmark
Finally, we measure. A JMH benchmark that builds a map of 200,000 realistic keys and times get() for hits and misses shows the difference directly; with a fix like this, the gap is often one or two orders of magnitude. JMH handles warm-up and dead-code elimination, which a hand-written timing loop gets wrong. We then confirm in a production-like load test that CPU per request drops and that the TreeNode.find frames disappear from the flame graph.
7Follow-up questions
Why does a non-Comparable key make tree bins slower?
A tree bin orders nodes by hash first. When two keys have equal hashes, the only way to choose left or right is compareTo, and HashMap uses it only if the class implements Comparable to itself and both keys are of the same class. Otherwise, during insertion it uses a tie-break on class name and identity hash, but a lookup cannot use that, because the probe key is a different object with a different identity hash. So find() searches one subtree recursively and then the other, calling equals() as it goes, which for many equal hashes approaches a linear scan of the bin.
Could a slow equals() alone explain the profile?
It could make equals() frames wide, but it would not produce TreeNode.find frames, because tree bins only appear when bins get long. A slow equals() with healthy hashing shows up as equals() under getNode with short chains, and the fix is different: make equals() cheaper, for example by comparing the most selective field first.
Is it safe to change hashCode() in a running system?
Within one JVM, yes, because maps are rebuilt on start-up. It is not safe if hash values are persisted or shared across processes, for example used for partitioning, sharding or cache keys in another system. Those uses should rely on an explicit, stable key format rather than hashCode().
8Conclusion
In this answer, we saw that HashMap$TreeNode.find frames mean bins have been treeified, so many OrderKey instances share a hash or a bucket. The likely cause is a hashCode() that uses too few fields or a low-cardinality field, made worse when OrderKey is not Comparable, because the tree then cannot use compareTo and falls back to calling equals() across subtrees. We confirmed it by counting distinct hash codes and bucket sizes on real keys, fixed hashCode() to combine all fields used by equals(), added a Comparable consistent with equals, and verified the result with JMH and a production-like load test.
Open with
The TreeNode.find frames are the key clue. A HashMap bin only becomes a red-black tree when it holds at least 8 nodes in a table of 64 or more, and with 200,000 well-hashed keys in a table of about half a million slots that should essentially never happen.
Then, if they want more
So many keys share a hash or a bucket. My first suspect is OrderKey.hashCode, for example hashing only a region or a status, so thousands of different keys get the same hash.
The second is that OrderKey is not Comparable: inside a tree, keys with equal hashes are ordered with compareTo, and without it find cannot choose a side, so it searches both subtrees and calls equals over and over, which matches the wide equals frames.
To confirm, I would take a sample of real keys and count distinct hashCode values, and compute the bucket distribution with the same spread and mask HashMap uses; a few distinct hashes or a largest bucket in the thousands proves it.
The fix is to make hashCode combine every field that equals uses, ideally by turning OrderKey into a record, and if the keys have a natural order, to implement Comparable consistently with equals.
I would add a unit test on the bucket distribution, and benchmark get for hits and misses with JMH before and after, then check CPU in a production-like load test.
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
- 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 profile of a service shows most CPU time in HashMap.getNode and in equals() of a key...": https://scrivio-interview-library.netlify.app/java/hashmap-internals/8a81dec2f8/