try other way for remove duplicate characters in String using Stream API: String s ="Eccclipseee"; s.chars().mapToObj(c->(char)c).collect(Collectors.toCollection(LinkedHashSet::new)).forEach(System.out::print);
We can do the same by doing this way.. String s = "Ecclipsee"; Map dupChar = Arrays.stream(s.split("")).map(String::toLowerCase).filter(f -> Collections.frequency(s, f) > 1).collect(Collectors.groupingBy(str -> str, Collectors.counting())); //This will pring the duplicate character with it's count. System.out.println(dupChar); {e=3, c=2} If you want to print only character and not the count, you need to traverse the map and print the key only..
LinkedHashSet maintain order. So while we removing duplicate characters recommended LinkedHashSet because of after removing duplicates sequential characters will come. Try with HashSet with some name then see output meaningless word will come. Keep Watching Java concepts by Jay tutorial.
try other way for remove duplicate characters in String using Stream API:
String s ="Eccclipseee";
s.chars().mapToObj(c->(char)c).collect(Collectors.toCollection(LinkedHashSet::new)).forEach(System.out::print);
Nice explanation .please keep uploading questions like these specially collections,stream API , lamda expression.
😊 Keep watching Java concepts by Jay tutorial. I’ll do more videos.
We can do the same by doing this way..
String s = "Ecclipsee";
Map dupChar = Arrays.stream(s.split("")).map(String::toLowerCase).filter(f -> Collections.frequency(s, f) > 1).collect(Collectors.groupingBy(str -> str, Collectors.counting()));
//This will pring the duplicate character with it's count.
System.out.println(dupChar);
{e=3, c=2}
If you want to print only character and not the count, you need to traverse the map and print the key only..
We can use list also with distinct() method..
We can do it using HasSet as well, Why LinkedHashSet please?
LinkedHashSet maintain order. So while we removing duplicate characters recommended LinkedHashSet because of after removing duplicates sequential characters will come. Try with HashSet with some name then see output meaningless word will come. Keep Watching Java concepts by Jay tutorial.