LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

What is a collision, and how does separate chaining handle it?

A collision is two different keys hashing to the same index. Separate chaining makes each cell hold a list of all entries that hash there, so colliding entries simply share the cell's list.

An eleven-cell table where cells 4 to 7 hold chains of several keys and most other cells are empty

* Colliding keys hang off the same cell in a list; lookups scan only that one short list. *

Collisions are unavoidable: there are far more possible keys than cells, so by the pigeonhole principle some keys must share an index. Nor can a hash function steer around occupied cells: it computes from the key alone and knows nothing about which cells of the array are already taken.

With separate chaining (also called closed addressing, because every entry stays at its home index), cell i points to a small list, the bucket:

  • put(k, v): hash to i, search i's list for k; replace the value if found, else append.
  • get(k) / remove(k): hash to i, then search only i's list.

Trade-offs:

  • Pro: simple, and the table never "fills up"; chains just get longer. Deleting is plain list removal.
  • Con: each bucket needs an extra data structure (the list), which costs memory and pointer-chasing.

The cost of an operation is the length of one chain. If n entries are spread evenly over N cells, a chain holds on average α = n/N entries, so operations are expected $O(1 + α)$.

Java's HashMap uses chaining; since Java 8 a chain that grows past 8 entries (in a table of at least 64 cells) is converted into a small balanced tree.

From Quiz: ADS / Sets, Maps and Hash Tables | Updated: Nov 26, 2026