Put your HashMap skills to the test! Parse a sentence, count how often each word appears, and display the results.
YOUR TASK
Given the sentence "the cat sat on the mat the cat", create a program that:
Splits the sentence into words using split(" ")
Counts each word's occurrences using a HashMap<String, Integer>
Prints each word and its count (one per line, in any order)
Need a hint?
Use map.getOrDefault(word, 0) + 1 to increment the count.
To print: for (String key : map.keySet()) { System.out.println(key + ": " + map.get(key)); }
Don't forget import java.util.HashMap;
Expected Output (any order):
the: 3
cat: 2
sat: 1
on: 1
mat: 1
Code Editor Ready
Output
Console
Console initialized...
Ready for Project 4: Word Frequency Counter
> HashMap is perfect for counting — word maps to its count
> Output order may vary, that's totally fine!