HashMaps help students move beyond repeated searching. Instead of scanning a list again and again, a map can store a relationship between a key and a value. This is especially useful for frequency counting and grouping.
Frequency maps
A frequency map stores how many times each value appears.
Map<String, Integer> counts = new HashMap<>();
for (String word : words)
{
if (!counts.containsKey(word))
counts.put(word, 0);
counts.put(word, counts.get(word) + 1);
}
This pattern replaces a nested loop that would otherwise count each word by scanning the whole list.
Why maps feel different
Arrays and ArrayLists use positions. Maps use keys. A key might be a word, a student ID, a tag name, or a category. The value might be a count, a total, a list, or an object.
Common mistakes
- Calling
getbefore checking whether the key exists. - Confusing a key with its stored value.
- Using a map when order matters but not thinking about iteration order.
- Forgetting to update the value after reading it.
Practice idea
Use a fictional list of club signups and count how many students chose each activity. Then extend the map so each activity points to a list of student names. That one upgrade moves students from counting to grouping.
