Skip to content

Commit bbeeef9

Browse files
committed
Add FirstNonRepeatedChar class to find the first non-repeated character in a string
1 parent 012df35 commit bbeeef9

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedHashMap;
5+
import java.util.stream.Collectors;
6+
7+
public class FirstNonRepeatedChar {
8+
public static void main(String[] args) {
9+
String str = "programming";
10+
Arrays.stream(str.split(""))
11+
.collect(Collectors.groupingBy(x -> x, LinkedHashMap::new, Collectors.counting()))
12+
.entrySet().stream().filter(x -> x.getValue() == 1)
13+
.map(x -> x.getKey())
14+
.findFirst().ifPresent(System.out::println);
15+
}
16+
}

0 commit comments

Comments
 (0)