What is the load factor in a HashMap, and what is the trade-off when you increase or decrease it?
answer
- Load factor = entries per bucket before resizing (default 0.75)
- Higher LF = less memory, more collisions, slower lookups
- Lower LF = more memory, faster lookups, more resizes
- Trades space vs time
- Java 8 treeifies long chains (>8), for bad hashes
basics
~20 sThe load factor is how full the bucket array is allowed to get (default 0.75) before the map grows. A higher load factor uses less memory but causes more collisions (slower lookups); a lower one speeds lookups but wastes memory.
solid answer
~50 sA HashMap stores entries in an array of buckets; the load factor sets how many entries per bucket are allowed before the map doubles its array, which happens when size exceeds capacity * loadFactor. The default, 0.75, is a deliberate balance of time against space. Raising it (say 0.9) packs entries more tightly — lower memory footprint — but increases the chance that two keys land in the same bucket, lengthening lookup chains and slowing get/put. Lowering it (say 0.5) keeps buckets sparse, so collisions and lookup times drop, at the cost of more memory, earlier resizes, and slower iteration, which takes time proportional to capacity plus size. In practice the default is almost always right; you'd only deviate for an extreme memory-constrained scenario (higher) or a latency-critical, read-heavy map (lower). Since Java 8, long collision chains convert to balanced trees, which softens pathological hashing; at ordinary load factors bins rarely get that long.
go deeper
Knows the load factor controls when the map grows and that 0.75 is the default; can state higher = less memory, lower = faster at a high level.
Articulates the space-vs-time trade-off precisely and connects load factor to collisions and resize timing; knows you set it in the constructor.
Cites the Javadoc's time/space tradeoff for 0.75, knows the source's Poisson figures show why tree bins are rare, and judges that presizing usually matters more than tuning the load factor.
Decides as policy whether to ever deviate, considering GC, cache behaviour, and real benchmarks; pushes back on premature load-factor tuning and standardizes presizing conventions.
## Buckets and hashing — the foundation A `HashMap` keeps entries in an internal array called the **bucket array** (or table). To find where a key goes, the map computes the key's **hash code**, then maps it to an index in that array. Ideally every key lands in its own bucket and lookup is O(1). But two different keys can map to the **same** bucket — a **collision**. Colliding entries are chained together in that bucket (a linked list, or since Java 8 a balanced tree once a chain gets long). The more crowded the buckets, the more collisions, and the longer those chains — so lookups degrade toward O(chain length). ## What the load factor is The **load factor** is a positive number, usually below 1 though `new HashMap<>(16, 2.0f)` is legal, that says: *how many entries per bucket may the table hold before we grow it?* It counts entries, not occupied buckets. The map resizes (doubles the bucket array and re-distributes every entry — "rehashing") when: ``` size > capacity * loadFactor ``` The default load factor is **0.75**. With the default capacity of 16, the map grows once it holds more than 12 entries. ## The trade-off, intuitively Think of buckets as shelves and entries as boxes, where several boxes can share a shelf: - **High load factor (e.g. 0.9):** you allow 0.9 boxes per shelf before building more shelves. **Less memory** (fewer shelves), but boxes crowd together — more collisions, longer chains, **slower** lookups and inserts. - **Low load factor (e.g. 0.5):** you build more shelves at only 0.5 boxes per shelf. Boxes are spread out — **fewer collisions, faster** access — but you keep many empty shelves (**more memory**), you trigger growth/rehashing **more often** as you fill it, and iteration slows, because it takes time proportional to capacity plus size. So the load factor trades **space against time**: | Load factor | Memory | Collisions / lookup time | Resize frequency | |---|---|---|---| | Low (0.5) | More used | Fewer / faster | More frequent | | Default (0.75) | Balanced | Balanced | Balanced | | High (0.9) | Less used | More / slower | Less frequent | ## Why 0.75 is the default The Javadoc's whole justification is that 0.75 "offers a good tradeoff between time and space costs". OpenJDK's source adds numbers, in a comment about tree bins rather than about choosing 0.75: under random hash codes, bin sizes follow a Poisson distribution with a parameter of about 0.5 on average for this threshold, with a large variance because of resizing granularity. On those figures about 61% of bins are empty, 30% hold one entry, and a bin of 8 has a probability of about 0.00000006. On the memory side, a table filled right to the threshold has one third more buckets than entries (1 / 0.75), and about half its buckets are still empty. ## Java 8 treeification and the high end Since Java 8, when a single bucket's chain exceeds 8 entries (and the table is at least 64 buckets), that chain is converted from a linked list into a **red-black tree**, so lookup in that bucket is O(log n) instead of O(n) when keys have distinct hash codes or are `Comparable`. That guards against bad hash functions more than against a high load factor: with good hashes, bins that long stay rare at 0.75 or 0.9, so the memory-vs-collision trade-off is untouched in the common case. A tree node is about twice the size of a plain node, so a load factor high enough to make trees common can use more memory than the default, not less. ## When to change it - **Raise it** only when memory is tight and you can tolerate slightly slower access (e.g. a huge map where footprint dominates). - **Lower it** only for a latency-critical, read-heavy map where you'll happily spend memory for fewer collisions. - **Otherwise: leave it at 0.75.** It is almost always correct, and most real wins come from setting the *initial capacity*, not from tuning the load factor. You set it via the constructor: `new HashMap<>(initialCapacity, loadFactor)`.
- If memory is extremely constrained, would you raise or lower the load factor, and what do you give up?Raise it (e.g. toward 0.9). Buckets pack tighter so the array is smaller for the same data. You give up speed: more collisions mean longer bucket chains and slower get/put. Java 8 tree bins help only a bin that grows past 8 entries, which stays rare at such load factors with good hashes.
- Why doesn't changing the load factor affect the correctness of lookups?Lookups still hash the key, go to the right bucket, and scan/search that bucket for an equal key. Collisions only lengthen the search within a bucket; the right entry is always found. For lookups, the load factor only influences how crowded buckets get, i.e. performance.
- Saying a higher load factor makes lookups faster — it makes them slower (more collisions)
- Confusing load factor (a ratio) with capacity (the array length)
- Claiming you should usually tune the load factor — the default 0.75 is almost always right; presizing matters more
- Thinking collisions make the map incorrect — they only make it slower; correctness is preserved
- HashMap tree bins
- Java 1.8
Assumes JDK 27. The 0.75 default, the resize rule and the tree-bin thresholds are the same at JDK 25; tree bins date from Java 8.
context
- HashMap (Java SE 27 & JDK 27)docs.oracle.com
- HashMap.java at jdk-27-gagithub.com