Java example: HashMap word count

Count word occurrences using a HashMap in Java.

Counts how often each word appears using a HashMap<String, Integer>. The counts.merge(w, 1, Integer::sum) call inserts 1 for new words and adds 1 to the existing count otherwise, a concise alternative to get-or-default logic. Wrapping the map in a TreeMap sorts the entries by key so the output printed with System.out.printf is deterministic.

Output

Errors