Java Program to Compute Word Frequency
Counts word occurrences in a sentence.
import java.util.*;
public class Main {
public static void main(String[] args) {
String s = "to be or not to be";
Map<String,Integer> freq = new LinkedHashMap<>();
for(String w : s.split(" ")){
freq.put(w, freq.getOrDefault(w, 0) + 1);
}
System.out.println(freq);
}
}{to=2, be=2, or=1, not=1}