There are following way to Print Distinct Characters from a String in Java 8.
- First get the Stream data from List using arrayList.stream() method.
- Using flatMapToInt to convert all list of string into character.
- Here mapToObj(r->(char)r) convert each character into object.
- Inside collect(collectors.toset()) method collect the distinct character.
- Print the Distinct character.
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class FindDistinctCharacter {
public static void main(String[] args) {
List<String> strings = Arrays.asList("java", "spring", "hibernate");
Set<Character> ch = strings.stream().flatMapToInt(String::chars).mapToObj(r -> (char) r)
.collect(Collectors.toSet());
System.out.println("distinct character from list of String are :- " + ch);
}
}
Output :-
distinct character from list of String are :- [a, b, e, g, h, i, j, n, p, r, s, t, v]