Learnitweb

Count Words in a String Using Java Streams

Assume a String text:

String text = "Java streams make processing strings easy and efficient.";

Approach 1: split() + groupingBy

Map<String, Long> counts = Arrays.stream(text.split("\\s+"))
    .map(String::toLowerCase)
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Approach 2: Pattern + Matcher.results() (Unicode-aware, Java 9+)

Pattern p = Pattern.compile("\\p{L}+"); // Unicode letters
Map<String, Long> counts2 = p.matcher(text).results()
    .map(m -> m.group().toLowerCase())
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));