🎓 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
1. Introduction
Counting the occurrence of words in a text is a common task in programming, especially in fields like data analysis, natural language processing, and search engine optimization. This process involves analyzing a string of text, identifying individual words, and counting how many times each word appears. Java's HashMap is an ideal data structure for this task, as it allows for fast lookups and can map each unique word to its corresponding count. This blog post will demonstrate how to use a HashMap in Java to find and count the occurrence of each word in a given string.
2. Program Steps
1. Define a string containing the text to be analyzed.
2. Split the string into words based on spaces.
3. Create a HashMap to store each word and its count.
4. Iterate over the array of words, updating the count for each word in the HashMap.
5. Display the words and their counts.
3. Code Program
import java.util.HashMap;
import java.util.Map;
public class WordOccurrenceInString {
public static void main(String[] args) {
// Step 1: Defining the string to be analyzed
String text = "this is a test this is only a test";
// Step 2: Splitting the string into words
String[] words = text.split(" ");
// Step 3: Creating a HashMap to store word counts
Map<String, Integer> wordCounts = new HashMap<>();
// Step 4: Iterating over the words and counting occurrences
for (String word : words) {
if (wordCounts.containsKey(word)) {
// If the word is already in the map, increment its count
wordCounts.put(word, wordCounts.get(word) + 1);
} else {
// Otherwise, add the word to the map with count 1
wordCounts.put(word, 1);
}
}
// Step 5: Displaying the word counts
System.out.println("Word occurrences:");
for (Map.Entry<String, Integer> entry : wordCounts.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
Word occurrences: this: 2 is: 2 a: 2 test: 2 only: 1
Explanation:
1. The program begins with a string text that contains the text to be analyzed. This string includes repeated words to demonstrate the counting process.
2. The split(" ") method is used to divide the string into an array of words, using spaces as delimiters.
3. A HashMap named wordCounts is then created. This HashMap will store each unique word as a key and the number of times it appears in the string as its value.
4. The program iterates over the array of words using a for-each loop. For each word, it checks if the word is already a key in the HashMap. If so, it increments the count for that word. If not, the word is added to the HashMap with an initial count of 1.
5. Finally, the program iterates over the entries in the HashMap and prints out each word and its count. The output demonstrates that the program successfully counts the occurrence of each word in the given string.
6. This example illustrates how to use a HashMap in Java to efficiently count the occurrences of words in a string, showcasing a fundamental technique for text analysis.
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