💡 Java Streams – Find First Non-Repeating Character

java dev.to

Input: Programming
Output: P
🔹 Step 1: Convert String to Character Stream
.word.chars()->Converts the string into an IntStream (ASCII values of characters)
🔹 Step 2: Convert int → Character
.mapToObj(ch -> (char) ch)
Converts each ASCII value into a Character object
🔹 Step 3: Group and Count Characters
.collect(groupingBy(Function.identity(), counting()))
Groups characters by themselves
Counts how many times each character appears
📌 Example Output:
{a=2, b=1, c=1}
🔹 Step 4: Filter Non-Repeating Characters
.filter(entry -> entry.getValue() == 1)
Keeps only characters that appear once
🔹 Step 5: Extract Keys (Characters)
.map(Map.Entry::getKey)
🔹 Step 6: Get First Non-Repeating Character
.findFirst().get();
Returns the first unique character

Source: dev.to

arrow_back Back to Tutorials