Java8 Streams Interview Question-18-Concatenate two list of strings & remove duplicates -by Naren

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ธ.ค. 2024

ความคิดเห็น •

  • @NarenJavaTech
    @NarenJavaTech  7 หลายเดือนก่อน

    Q No: 18:Given two lists of strings, concatenate them and remove duplicates using stream API.
    Code:
    =====
    List list1 = List.of("apple", "banana", "orange", "pinaaple");
    List list2 = List.of("banana", "kiwi", "grape", "guava", "apple");
    Stream.concat(list1.stream(), list2.stream()).
    distinct().
    forEach(System.out::println);

    • @YogendraSingh-gv9qt
      @YogendraSingh-gv9qt 5 หลายเดือนก่อน

      List list1 = Arrays.asList("apple","hina","banana","didi","didi");
      List list2 = Arrays.asList("singh","hina","banana","didi","didi");
      List collect = Stream.concat(list1.stream(),list2.stream()).distinct().collect(Collectors.toList());
      System.out.println(collect);