Assume a paragraph:
String paragraph = "Java streams make processing strings easy. Streams in Java are powerful.";
Approach 1: Using split() + groupingBy
Map<String, Long> wordCount = Arrays.stream(paragraph.split("\\s+"))
.map(word -> word.replaceAll("\\p{Punct}", "").toLowerCase()) // remove punctuation
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(wordCount);
Approach 2: Using Pattern + Matcher.results() (Unicode-aware, Java 9+)
Pattern pattern = Pattern.compile("\\p{L}+"); // matches Unicode letters
Map<String, Long> wordCount2 = pattern.matcher(paragraph).results()
.map(m -> m.group().toLowerCase())
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(wordCount2);
