Java 8 Program To Count Characters in a String

1. Introduction

Identifying duplicate characters in a string is a common text processing task, which can be useful in various contexts such as data validation, syntax checking, or as part of algorithms in larger applications. Java 8 introduced the Streams API, which provides a powerful set of tools for handling collections and streamlining operations like filtering, sorting, and summarizing data. This blog post will demonstrate how to use Java 8 features to print duplicate characters in a string, showcasing an efficient and concise approach.

Java 8 Program To Count Characters in a String

2. Program Steps

1. Convert the string into a character stream.

2. Collect characters into a map with their occurrence count.

3. Filter the map to retain only those characters that occur more than once.

4. Print the duplicate characters.

3. Code Program

import java.util.function.Function;
import java.util.stream.Collectors;

public class PrintDuplicateCharacters {
    public static void main(String[] args) {
        String input = "programming"; // The input string

        // Step 1: Converting the string into a stream of characters
        input.chars() // Creates an IntStream
             .mapToObj(c -> (char) c) // Convert int stream to Character stream
             // Step 2: Collecting characters with their occurrence count
             .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
             // Step 3: Filtering to retain only duplicates
             .entrySet().stream().filter(entry -> entry.getValue() > 1)
             // Step 4: Printing duplicate characters
             .forEach(entry -> System.out.println("Character: " + entry.getKey() + ", Count: " + entry.getValue()));
    }
}

Output:

Character: g, Count: 2
Character: r, Count: 2
Character: m, Count: 2

Explanation:

1. The program starts with an input string input containing the word "programming". The goal is to find and print the characters in this string that appear more than once.

2. It converts the string into a stream of characters using input.chars(), which returns an IntStream representing the character values. This stream is then converted into a stream of Character objects for further processing.

3. The stream is collected into a Map<Character, Long> using Collectors.groupingBy(), which groups characters by their identity (i.e., the character itself) and counts their occurrences using Collectors.counting().

4. The resulting map entries are then streamed, and a filter is applied to retain only those entries with a count greater than 1, indicating duplicate characters.

5. Finally, for each entry in the filtered stream (representing a duplicate character), the character and its count are printed to the console. The output clearly shows the characters 'g', 'r', and 'm' as duplicates and their occurrence counts, demonstrating the efficiency and expressiveness of Java 8's Streams API for processing and analyzing strings.

Comments