Counting Words in a String Using HashMap in Java

Respondido usando: GPT-4o-mini porJava Q&A Hub

Pergunta:

Find the word count in a string using HashMap Collection.

Resposta:

To find the word count in a string using a HashMap in Java, you can follow these steps:

  1. Split the string into words.
  2. Use a HashMap to store each word as a key and its count as the value.
  3. Iterate through the array of words, updating the count in the 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));
        }
    }
}

Explanation:

  1. Normalization: The input string is converted to lower case and punctuation is removed to ensure that words are counted consistently (e.g., "Test" and "test" are treated as the same word).
  2. Splitting: The string is split into words using whitespace as the delimiter.
  3. Counting: A 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.
  4. Output: Finally, the word counts are printed.

Note:

  • This code assumes that words are separated by spaces and that punctuation is not part of the words. Adjustments may be needed based on specific requirements or input formats.