1.Word frequency
IntermediateCount the words in "red blue red green blue red" and print the counts in alphabetical order.
Show hint
A TreeMap keeps keys sorted; merge(word, 1, Integer::sum) adds one per occurrence.
Show solution
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> counts = new TreeMap<>();
for (String w : "red blue red green blue red".split(" ")) {
counts.merge(w, 1, Integer::sum);
}
System.out.println(counts);
}
}{blue=2, green=1, red=3}