remove duplicate characters | print duplicate characters using stream api | stream api interview Q

Поделиться
HTML-код
  • Опубликовано: 30 ноя 2024

Комментарии • 7

  • @JavaconceptsbyJaytutorial
    @JavaconceptsbyJaytutorial  3 месяца назад +2

    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);

  • @saurabhverma3864
    @saurabhverma3864 3 месяца назад

    Nice explanation .please keep uploading questions like these specially collections,stream API , lamda expression.

  • @randhirkumar6262
    @randhirkumar6262 2 месяца назад

    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..

  • @TheTravellerVlogs99
    @TheTravellerVlogs99 3 месяца назад

    We can use list also with distinct() method..

  • @dheerajmadan1439
    @dheerajmadan1439 3 месяца назад

    We can do it using HasSet as well, Why LinkedHashSet please?

    • @JavaconceptsbyJaytutorial
      @JavaconceptsbyJaytutorial  3 месяца назад

      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.