HashMap stores key-value pairs in an array of buckets. It calls hashCode() on the key, mixes the bits, and uses the result to pick a bucket index. Keys that land in the same bucket are chained in a linked list, and since Java 8 a long chain is converted into a balanced red-black tree. That is how hashmap works internally, and it is why get and put run in average constant time.
This guide covers the Java 8 and later implementation (the same design is used in Java 21 and 25). The details are implementation behavior, not a specification, so treat the exact thresholds as current defaults rather than guarantees.
Inside HashMap there is a field Node<K,V>[] table. Each slot of the array is a bucket. The table is allocated lazily on the first put, with a default capacity of 16. The capacity is always a power of two.
Every entry is a Node that holds four things: the cached hash, the key, the value, and a next reference to the following node in the same bucket.
When a bucket holds only one entry, it is a single node. When two keys collide, the nodes are linked through next.
A key's hashCode() is a 32-bit int, but the table is much smaller, so the hash has to be reduced to an index. HashMap does this in two steps.
h ^ (h >>> 16). This mixes the high 16 bits into the low 16 bits, so keys whose hash codes differ only in the upper bits still spread across buckets.index = (n - 1) & hash, where n is the capacity.Because n is a power of two, (n - 1) & hash is the same as hash % n for non-negative values, but it is faster. A null key is allowed: its hash is defined as 0, so it always goes to bucket 0.
This prints the bucket for each key (the numbers depend on String.hashCode()) and then 1 for the null key.
For put(key, value) the map follows these steps:
key == node.key or key.equals(node.key). If a match is found, replace the value and return the old one.For get(key) the map computes the same index and walks the bucket with the same comparison. This is why a key class needs a consistent equals and hashCode pair: two equal keys must produce the same hash, or they land in different buckets and the lookup misses.
Two different keys can map to the same bucket. That is a collision, and it is normal. With a good hash function, chains stay very short.
If a chain grows, lookups degrade from constant time toward linear time. To limit the damage, Java 8 added treeification:
| Condition | Result |
|---|---|
| A bucket reaches 8 nodes and the table has 64 or more buckets | The chain becomes a red-black tree of TreeNode objects |
| A bucket reaches 8 nodes but the table has fewer than 64 buckets | The table is resized instead |
| A tree shrinks to 6 nodes or fewer (after removals or a resize split) | The tree converts back to a linked list |
Tree lookups cost O(log n) instead of O(n). If the keys implement Comparable, the tree uses that ordering; otherwise it falls back to a tie-breaking order based on class name and identity hash, and may need to search both subtrees. This protects against hash-flooding attacks and badly written hashCode methods, but it is a safety net, not a replacement for a good hash.
The load factor is 0.75 by default. The threshold is capacity * loadFactor, so a default map resizes when it holds more than 12 entries. Resizing doubles the table (16, 32, 64, and so on) and redistributes the nodes.
Because the capacity is a power of two and it doubles, each node either stays at its old index or moves to oldIndex + oldCapacity. The resize code tests one extra bit of the hash to decide which, and it keeps the relative order of nodes inside each bucket. No hash is recomputed, since the hash is cached in the node.
Resizing is the expensive part of put, because every entry is touched. If you know the expected size, size the map up front. Passing a capacity to the constructor treats it as the table size, so allow for the load factor, or use the factory method added in Java 19:
String, Integer, records or enums. If a key's fields change after insertion, its hash changes and the entry becomes unreachable.hashCode and equals together. Records do this for you.HashMap is not thread-safe. Concurrent writes can lose entries or corrupt the structure. Use ConcurrentHashMap for shared mutable maps.LinkedHashMap for insertion order or TreeMap for sorted order.get, put and remove are O(1) on average. In the worst case, with many collisions, a treeified bucket gives O(log n) for keys that are comparable, and a linked bucket gives O(n).
It lets the index be computed with a bit mask, (n - 1) & hash, instead of a division, and it makes resizing simple because each node stays put or moves by exactly the old capacity.
Yes. One null key is allowed and is stored in bucket 0, and any number of null values are allowed. Hashtable and ConcurrentHashMap reject null.
They go to the same bucket. HashMap then uses equals to tell them apart, so both entries are kept as separate nodes in that bucket.
HashMap is an array of buckets holding Node chains. The key's hash is spread and masked to choose a bucket, equals resolves collisions, buckets with 8 or more nodes can become red-black trees when the table has at least 64 buckets, and the table doubles when it passes 75 percent load. Good immutable keys, a correct hashCode, and pre-sizing give the best performance.