Quiz Entry - updated: 2026.12.01
What are typical applications of hash tables, and when is a search tree the better choice for a map?
Hash tables give expected $O(1)$ lookups: symbol tables in compilers, caches, small in-memory databases, sets of seen items. A search tree is better when you need the keys in order: sorted iteration, min and max, or range queries.
Hashing scatters keys on purpose, so a hash table cannot answer "what is the next larger key?" or "list all keys between 100 and 200" without scanning everything.
| Need | Hash table | Balanced search tree |
|---|---|---|
| get / put / remove | $O(1)$ expected, $O(n)$ worst | $O(\log n)$ guaranteed |
| iterate in key order | no (sort first) | yes, inorder |
| min, max, next larger key | $O(n)$ | $O(\log n)$ |
| range query | $O(n)$ | $O(\log n + $ results$)$ |
| key requirement | equals + hashCode |
a total order (compareTo) |
Typical uses of hash tables:
- compilers: symbol tables that map identifiers to their declarations,
- browser and web caches: URL → stored response,
- small databases and indexes on an exact key,
- deduplication: "have I seen this element before?" in $O(1)$.
Java offers both behind the same interface: HashMap and TreeMap; Python's built-in dict and set are hash tables.