I need to find repeated words on a string, and then count how many times they were repeated. So basically, if the input string is this:
String s = \"House, House
Using Java 8 streams collectors:
public static Map countRepetitions(String str) { return Arrays.stream(str.split(", ")) .collect(Collectors.toMap(s -> s, s -> 1, (a, b) -> a + 1)); }
Input: "House, House, House, Dog, Dog, Dog, Dog, Cat"
"House, House, House, Dog, Dog, Dog, Dog, Cat"
Output: {Cat=1, House=3, Dog=4}
{Cat=1, House=3, Dog=4}