HashMap
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
Under the hood. A HashMap is an array of buckets (16 by default). The bucket number comes from the key's hashCode().
When there are more elements than 75% of the number of buckets, the array doubles and everything is redistributed. If many keys pile up
in one bucket (more than 8), Java turns it from a list into a tree — so even with a bad hash,
searching doesn't degrade to O(n).
The iteration order of a HashMap is undefined and can change as it grows — don't rely on it in your output.
A classic is counting frequencies:
Map<String, Integer> freq = new HashMap<>();
for (String w : words)
freq.merge(w, 1, Integer::sum); // no key — 1, there is one — add 1
// the same: freq.put(w, freq.getOrDefault(w, 0) + 1);
Task. Count how many times each word appears (ignoring case). Print the most frequent
word and its count (on a tie — the one that comes first alphabetically), then all the words in alphabetical order as word=N,
separated by spaces.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.