Introduction
In this guide, we will write a simple Java 8 program to count the occurrences of each word in a string. This problem is a common task in text processing and can be easily solved using the Java Stream API. The program will count how many times each word appears and then print the result.
Problem Statement
Write a Java program that:
- Takes a sentence (string) as input.
- Splits the string into individual words.
- Counts how many times each word appears in the string.
- Displays the result.
Example:
- Input:
"Java is awesome, Java is simple"
- Output:
java : 2
is : 2
awesome : 1
simple : 1
Solution Steps
- Define the Input String: We will provide a predefined sentence as the input.
- Convert the Sentence into Words: Split the sentence into individual words using the
split()
method. - Count the Words: Use Java 8 Streams and
Collectors.groupingBy()
to count how many times each word appears. - Display the Results: Print each word with its respective count.
Java Program
import java.util.*;
import java.util.stream.Collectors;
public class WordCount {
public static void main(String[] args) {
// Step 1: Define the input string
String input = "Java is awesome Java is simple";
// Step 2: Split the string into words and count occurrences
Map<String, Long> wordCount = Arrays.stream(input.toLowerCase().split("\\s+")) // Convert to lowercase and split by spaces
.collect(Collectors.groupingBy(word -> word, Collectors.counting())); // Group and count occurrences
// Step 3: Display the result
wordCount.forEach((word, count) -> System.out.println(word + " : " + count));
}
}
Output
java : 2
is : 2
awesome : 1
simple : 1
Explanation
Step 1: Define the Input String
We start by defining an input string:
String input = "Java is awesome Java is simple";
This is the sentence that the program will process. Each word in this sentence will be counted.
Step 2: Split the String and Count Words
We use split("\\s+")
to split the string into individual words based on spaces:
Arrays.stream(input.toLowerCase().split("\\s+"))
The toLowerCase()
method is used to make the counting case-insensitive, so words like "Java" and "java" will be treated as the same word.
Then, Collectors.groupingBy()
is used to group the words, and Collectors.counting()
counts the occurrences of each word:
.collect(Collectors.groupingBy(word -> word, Collectors.counting()));
Step 3: Display the Results
We use the forEach
method to print each word and its count:
wordCount.forEach((word, count) -> System.out.println(word + " : " + count));
This will print the word followed by the number of times it appears in the input string.
Output Example
Example 1:
java : 2
is : 2
awesome : 1
simple : 1
Example 2:
For an input like "Java is simple and Java is awesome"
:
java : 2
is : 2
simple : 1
and : 1
awesome : 1
Conclusion
This Java 8 program shows how to count the occurrences of each word in a string using Streams. The program splits the sentence into words, counts them, and prints the results. This technique is useful for text-processing tasks, such as analyzing the frequency of words in a document.
Comments
Post a Comment
Leave Comment