There are following way to get matching characters in a string in java.
- We take a string word.
- Create a HashSet Object of String type.
- Now get the Stream data from Arrays.stream() method.
- Break the string word from split method.
- In filter method we are adding duplicate word inside HashSet.
- From Collectors.toSet() method we are collect only unique data.
- Now print the character value.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class MatchChar {
public static void main(String[] args) {
String str = "wweelcomee";
Set<String> hs = new HashSet<String>();
Set<String> matchChar = Arrays.stream(str.split("")).filter(s -> !hs.add(s))
.collect(Collectors.toSet());
System.out.println("matching character is:- " + matchChar);
}
}
output:- matching character is:- [e, w]