Java Program To Count the Occurrences of Each Character Using HashMap

1. Introduction

Counting the occurrences of each character in a string is a common programming task and is useful in various applications such as text processing, data analysis, and cryptography. One efficient way to achieve this in Java is by using a HashMap to map each character to its count of occurrences. This approach takes advantage of the HashMap's ability to store key-value pairs, where the key is the character and the value is the count. This blog post illustrates how to implement such a program in Java.

2. Program Steps

1. Create a HashMap to store characters as keys and their counts as values.

2. Convert the input string to a char array to facilitate iteration over each character.

3. Iterate over the char array, and for each character, update its count in the HashMap.

4. Display the contents of the HashMap, showing each character and its count.

3. Code Program

import java.util.HashMap;

public class CharacterCount {
    public static void main(String[] args) {
        // Define the input string
        String input = "example string";

        // Step 1: Create a HashMap to hold character counts
        HashMap<Character, Integer> charCounts = new HashMap<>();

        // Step 2: Convert the input string to a char array
        char[] characters = input.toCharArray();

        // Step 3: Iterate over the char array
        for (char ch : characters) {
            charCounts.put(ch, charCounts.getOrDefault(ch, 0) + 1);
            // This line checks if ch is already a key in the map. If so, it increments the count.
            // If not, it adds ch to the map with a count of 1.
        }

        // Step 4: Display the character counts
        System.out.println("Character counts:");
        for (HashMap.Entry<Character, Integer> entry : charCounts.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Character counts:
 : 1
a: 1
e: 2
g: 1
i: 1
l: 1
m: 1
n: 1
p: 1
r: 1
s: 1
x: 1

Explanation:

1. The program begins by defining an input string, "example string", to analyze.

2. A HashMap<Character, Integer> named charCounts is created. This HashMap is used to store the count of each character in the input string. The key is the character, and the value is the count.

3. The input string is converted to a char array using toCharArray(). This conversion allows the program to iterate over each character in the string.

4. The program then iterates over each character in the array. For each character, it uses the put method to update the HashMap. The getOrDefault method is used to retrieve the current count for a character, defaulting to 0 if the character is not yet in the map. The count is then incremented by 1.

5. Finally, the program iterates over the HashMap entries and prints each character and its count. The entrySet() method is used to get a set view of the map's contents, which is then iterated to print each character (key) and

Comments