Easy java streams

Count character frequency in a string

The 30 second answer

I turn the string into a stream of characters and collect with groupingBy and counting. That gives a map from character to frequency. Before coding I clarify case folding and whitespace, and whether they care about code points versus UTF-16 chars for non-BMP text.

Map<Character, Long> freq = s.chars()
    .mapToObj(c -> (char) c)
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Same codePoints() caveat as the first-non-repeated problem. Decide on case-folding and whitespace before coding.

Sources:

Sources (0)
See inline citations in the answer above.