Find the word count in a string using HashMap Collection.
To find the word count in a string using a HashMap
in Java, you can follow these steps:
HashMap
to store each word as a key and its count as the value.HashMap
.Here's a sample Java code that demonstrates this approach:
import java.util.HashMap;
public class WordCount {
public static void main(String[] args) {
String input = "This is a test. This test is only a test.";
countWords(input);
}
public static void countWords(String input) {
// Normalize the string by converting it to lower case and removing punctuation
String normalizedInput = input.toLowerCase().replaceAll("[^a-zA-Z0-9 ]", "");
// Split the string into words
String[] words = normalizedInput.split("\\s+");
// Create a HashMap to store word counts
HashMap<String, Integer> wordCountMap = new HashMap<>();
// Iterate through the words and count them
for (String word : words) {
if (wordCountMap.containsKey(word)) {
// If the word is already in the map, increment its count
wordCountMap.put(word, wordCountMap.get(word) + 1);
} else {
// If the word is not in the map, add it with a count of 1
wordCountMap.put(word, 1);
}
}
// Print the word counts
for (String word : wordCountMap.keySet()) {
System.out.println(word + ": " + wordCountMap.get(word));
}
}
}
HashMap
is used to keep track of the count of each word. If a word already exists in the map, its count is incremented; otherwise, it is added with a count of 1.