Introduction
Counting the occurrences of each word in a string is a common requirement in text processing, particularly in tasks like analyzing text data, creating word frequency tables, or identifying the most common words in a document. With Java 8, you can accomplish this efficiently using streams and collectors. This guide will demonstrate how to write a Java program that counts the occurrences of each word in a given string.
Problem Statement
The task is to create a Java program that:
- Takes a string as input.
- Splits the string into individual words.
- Uses Java 8 Streams to count how often each word appears.
- Displays the word and its corresponding count.
Example:
- Input:
"Java is great and Java is popular" - Output:
Word: Java, Count: 2; Word: is, Count: 2; Word: great, Count: 1; Word: and, Count: 1; Word: popular, Count: 1
Solution Steps
- Input String: Start with a string that contains words separated by spaces or other delimiters.
- Split the String into Words: Use the
split()method to break the string into individual words. - Stream Processing: Convert the array of words into a stream and use
Collectors.groupingByto group and count the words. - Display Results: Print each word and its corresponding count.
Java Program
Java 8 Program to Count Occurrences of Each Word in a String
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Java 8 Program to Count Occurrences of Each Word in a String
* Author: https://www.rameshfadatare.com/
*/
public class WordCountInString {
public static void main(String[] args) {
// Step 1: Take input string
String input = "Java is great and Java is popular";
// Step 2: Split the string into words and count occurrences using streams
Map<String, Long> wordCount = Arrays.stream(input.split("\\s+"))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
// Step 3: Display the word counts
wordCount.forEach((word, count) -> System.out.println("Word: " + word + ", Count: " + count));
}
}
Explanation of the Program
-
Input Handling: The input string
"Java is great and Java is popular"is used to demonstrate the word count functionality. -
Splitting the String: The
split("\\s+")method splits the string into words, where\\s+is a regular expression that matches any sequence of whitespace characters. -
Grouping and Counting: The
Collectors.groupingBy(Function.identity(), Collectors.counting())groups the words and counts how often each word appears in the string. -
Output: The program prints each word alongside the number of times it occurs in the input string.
Output Example
Example 1:
Word: Java, Count: 2
Word: is, Count: 2
Word: great, Count: 1
Word: and, Count: 1
Word: popular, Count: 1
Advanced Considerations
-
Case Sensitivity: The program is case-sensitive, so
Javaandjavaare considered different words. To make it case-insensitive, convert the input string to lower case usinginput.toLowerCase(). -
Handling Punctuation: The program counts words separated by spaces, but it does not handle punctuation marks. To include this, you can refine the
split()method or use additional string processing techniques to clean the input. -
Performance Considerations: The solution is efficient for typical text inputs and leverages the power of Java 8 Streams to process and count words effectively.
Conclusion
This Java 8 program provides a straightforward and efficient way to count the occurrences of each word in a string. Using streams and collectors makes the code clean and easy to understand, allowing you to quickly analyze text data in various applications.