🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 : 2is : 2awesome : 1simple : 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment