HashMap Time Complexity
Fast recall
Quick revision
Core answer
HashMap.get() and put() are expected O(1) with well-distributed hashes. Heavy collisions require scanning or navigating one bucket. Current JDKs can treeify a large bin when the table is large enough, often improving collision-heavy lookup toward O(log n) for suitably orderable keys, but the public HashMap contract does not promise a universal O(log n) worst case.Whether you know average vs worst case and the Java 8 treeification improvement
Saying HashMap is always O(1), or claiming treeification guarantees O(log n) for every key and operation
Separate expected lookup cost, collision-bin behavior, resizing, and O(capacity + size) iteration
Interview-ready guide
Interview answer
HashMap get and put are expected O(1) when keys have stable, well-distributed hashes. The hash takes the operation to one bin, and equals() identifies the key within that bin. If many keys collide, a linked bin can take O(n) to scan.
Current JDK implementations can treeify a busy bin when the table is large enough, often improving work in that bin toward O(log n). I would not call that a universal HashMap worst-case guarantee because the public API does not promise the internal representation and tree tie-breaking depends on the keys. I would also separate single-key lookup from a resize, which is occasional O(n), and full-map iteration, which depends on both capacity and size.
HashMap Time Complexity
Direct Answer
HashMap.get() and put() are expected O(1) with well-distributed hashes. Heavy collisions require scanning or navigating one bucket. Current JDKs can treeify a large bin when the table is large enough, often improving collision-heavy lookup toward O(log n) for suitably orderable keys, but the public HashMap contract does not promise a universal O(log n) worst case.
Code Example
Each put and get hashes the key straight to a bucket — no scan — which is why both are constant time on average.
import java.util.*;
public class HashMapComplexity {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
// put: hash the key -> index -> place in bucket. O(1) average.
map.put("sku-1", 100);
map.put("sku-2", 200);
// get: hash the key -> index -> read the bucket. O(1) average.
Integer price = map.get("sku-1"); // 100
// Worst case (all collide) is O(n), but since Java 8 a bucket
// with > 8 entries becomes a red-black tree -> O(log n).
// Note: iterating the whole map is always O(n + capacity).
for (var e : map.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}
}
}
The entrySet() loop is the exception — iterating the whole map is O(n + capacity), since it touches every bucket.
Common Mistakes & Pitfalls
- Saying always O(1): the correct description is expected O(1) with suitable hashes.
- Promising a universal O(log n) worst case: tree bins are an implementation strategy with key and operation nuances, not a public API guarantee.
- Ignoring mutable keys: a changed hash/equality value can make an entry unreachable by normal lookup.
- Confusing lookup and iteration: walking the whole map is O(capacity + size).
- Ignoring resize: growth is an occasional O(n) redistribution.
Check your understanding
Answer these without looking back at the article.
- Why is HashMap get O(1)?
- What is amortized O(1)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.