Java 8 Program To Find First Repeated Character in a String

1. Introduction

Identifying the first repeated character in a string is a common challenge in string manipulation and analysis tasks. This problem is particularly interesting in the context of parsing and lexical analysis, where the identification of duplicate characters can signify syntax errors or other anomalies. Java 8 introduced powerful new features for working with streams that make solving this problem more straightforward and expressive. This blog post will demonstrate how to find the first repeated character in a string using Java 8's Stream API, showcasing a functional approach to solving this common programming problem.

2. Program Steps

1. Convert the string to a stream of characters.

2. Use a LinkedHashMap to maintain the order of characters and count occurrences.

3. Filter the map to find the first character with more than one occurrence.

4. Print the first repeated character if present.

3. Code Program

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

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

        // Step 1: Convert the string into a stream of characters
        Character result = input.chars() // Creates an IntStream
                .mapToObj(c -> (char) c) // Convert int to Character
                // Step 2: Collect characters to map with occurrence count, preserving order
                .collect(Collectors.toMap(Function.identity(), v -> 1, Integer::sum, LinkedHashMap::new))
                // Step 3: Find the first character with more than one occurrence
                .entrySet().stream().filter(entry -> entry.getValue() > 1)
                .findFirst().orElse(null); // Retrieve the first repeated character, if any

        // Step 4: Print the first repeated character
        if (result != null) {
            System.out.println("First repeated character: " + result);
        } else {
            System.out.println("No repeated characters found.");


First repeated character: a


1. The program starts with a string input set to "javastream", where the task is to find the first character that appears more than once.

2. The string is converted into a stream of Character objects, which allows for functional-style operations on each character.

3. The characters are collected into a LinkedHashMap using Collectors.toMap(). This specific map implementation preserves the insertion order of keys and uses the merge function Integer::sum to count occurrences of each character. This is crucial for identifying the first repeated character while maintaining the original order of characters in the string.

4. The map's entry set is streamed, and a filter is applied to find the first entry where the value (the count) is greater than one, indicating a repeated character. The findFirst() terminal operation is then used to retrieve this character, if any.

5. Finally, the program checks if a repeated character was found and prints it. In this case, 'a' is the first character in "javastream" that appears more than once, as reflected in the output. This solution highlights the power of Java 8's Stream API and LinkedHashMap for efficiently solving problems related to string analysis and manipulation.