Hashing and HashMap
Lesson 13 of 20Follow a key through hashCode into its bucket, watch chains grow on collisions, and see a rehash in action.
Meet the hash map
A hash map turns a key into a number, a number into a bucket, and a bucket into O(1) lookups — on average. Collisions and load factor are the two things that keep it honest.
The hash idea
key.hashCode() gives a spread-out 32-bit int; the map folds it into a bucket with (n - 1) & hash. Same key, same bucket, every time.
Buckets & chains
Two keys can land in one bucket — a collision. Java chains them in a list inside the bucket and compares with equals().
Cost math
One hash plus one short chain is O(1) on average. If every key collides into one bucket, lookups degrade to O(n).
Load factor 0.75
When size passes capacity × 0.75, HashMap resizes to double the buckets and re-hashes every key.
Map vs Set
HashMap stores key → value pairs; HashSet is the same machinery with only keys. Both rely on hashCode and equals.
Buckets & collisions
h(k) = k % 7 — watch keys scatter, chains absorb collisions, and a lookup walk a chain.
Seven buckets, h(k) = k % 7, all empty. Keys 49, 15, 8, 30, 23, 41, 12 are waiting to be inserted.
Key to bucket path
Every get() and put() walks the same five-step road from key to value.
Walkthrough
Frequency counting with HashMap — then a duplicate scan with HashSet.
1import java.util.HashMap;2import java.util.Map;34public class FrequencyMap {5public static void main(String[] args) {6String[] words = {"cat", "dog", "cat", "bat", "cat"};7Map<String, Integer> count = new HashMap<>();89for (String w : words) {10int seen = count.getOrDefault(w, 0);11count.put(w, seen + 1);12}13System.out.println(count.get("cat"));14System.out.println(count.get("bat"));15System.out.println(count.get("fox"));16}17}
words has 5 strings — "cat" appears three times.
Resize & rehash
Load 1.0 crosses the 0.75 line — the table doubles and every key finds a new home.
Four buckets holding three entries (49, 30, 15) — load factor 3/4 = 0.75, right at the trigger.
Predict the output
put overwrites, get returns null for missing keys — trace each call.
1import java.util.HashMap;2import java.util.Map;34public class Predict {5public static void main(String[] args) {6Map<String, Integer> m = new HashMap<>();7m.put("a", 1);8m.put("b", 2);9m.put("a", 5);10System.out.println(m.get("a"));11System.out.println(m.get("c"));12System.out.println(m.size());13}14}
Hint: Putting the same key twice overwrites the value (keys stay unique). get on a missing key yields null, and size counts keys, not puts.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
HashMap = hash code, fold to a bucket, compare with equals — O(1) on average.
Collisions are normal; separate chaining keeps them cheap.
Past load factor 0.75 the table doubles and re-hashes every key.
put on an existing key overwrites; keys are always unique.
get returns null (not an exception) for missing keys — containsKey avoids ambiguity.
The frequency-count idiom: map.put(w, map.getOrDefault(w, 0) + 1).