Java Program to Count Occurrences of Each Character in a String

In this tutorial, we will learn how to write a Java program to count the occurrences of each character in a given string using different methods and techniques. We will cover the following methods to count the occurrences of each character in a string:
  • Using HashMap
  • Using Java 8 Streams
Let's start with the first method to count the occurrences of each character in a string using HashMap.

Java Program to Count Occurrences of Each Character in a String using HashMap

In this method, we will use a HashMap to count the occurrences of each character in a given string. We will iterate over each character in the string and store the count of each character in the HashMap. Finally, we will print the count of each character in the string. We will follow the below steps to count the occurrences of each character in a string using HashMap:

  1. Create a HashMap to store the count of each character in the string.
  2. Iterate over each character in the string.
  3. Check if the character is present in the HashMap.
  4. If the character is present, increment the count by 1.
  5. If the character is not present, add the character to the HashMap with count 1.
  6. Finally, print the count of each character in the string.
Let's see the Java program to count the occurrences of each character in a string using HashMap:
import java.util.HashMap;
import java.util.Map;

public class CountCharacters {
    public static void main(String[] args) {
        String str = "Hello, World!";
        Map charCountMap = new HashMap<>();

        for (char ch : str.toCharArray()) {
            if (Character.isLetter(ch)) {
                charCountMap.put(ch, charCountMap.getOrDefault(ch, 0) + 1);
            }
        }

        for (Map.Entry entry : charCountMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}
Output:
H : 1
e : 1
l : 3
o : 2
W : 1
r : 1
d : 1
Time Complexity: O(n) - where n is the length of the string as we are iterating over each character in the string.
Space Complexity: O(n) - where n is the length of the string as we are storing the count of each character in the HashMap.

Java Program to Count Occurrences of Each Character in a String using Java 8 Streams

In this method, we will use Java 8 Streams to count the occurrences of each character in a given string. We will convert the string to a character stream and group the characters by counting the occurrences of each character. Finally, we will print the count of each character in the string. We will follow the below steps to count the occurrences of each character in a string using Java 8 Streams:

  1. Convert the string to a character stream.
  2. Group the characters by counting the occurrences of each character.
  3. Finally, print the count of each character in the string.
Let's see the Java program to count the occurrences of each character in a string using Java 8 Streams:
import java.util.Map;
import java.util.stream.Collectors;

public class CountCharacters {
    public static void main(String[] args) {
        String str = "Hello, World!";
        Map charCountMap = str.chars()
                .mapToObj(c -> (char) c)
                .filter(Character::isLetter)
                .collect(Collectors.groupingBy(c -> c, Collectors.counting()));

        charCountMap.forEach((k, v) -> System.out.println(k + " : " + v));
    }
}
Output:
H : 1
e : 1
l : 3
o : 2
W : 1
r : 1
d : 1
Time Complexity: O(n) - where n is the length of the string as we are iterating over each character in the string.
Space Complexity: O(n) - where n is the length of the string as we are storing the count of each character in the Map.
In this method, we have used Java 8 Streams to count the occurrences of each character in a string. We have converted the string to a character stream using str.chars() and then filtered the characters using filter(Character::isLetter). Finally, we have grouped the characters by counting the occurrences of each character using Collectors.groupingBy and Collectors.counting().