Common elements in two lists

后端 未结 14 1155
无人共我
无人共我 2020-11-22 12:22

I have two ArrayList objects with three integers each. I want to find a way to return the common elements of the two lists. Has anybody an idea how I can achiev

14条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 12:55

    Below code Remove common elements in the list

    List result =  list1.stream().filter(item-> !list2.contains(item)).collect(Collectors.toList());
    

    Retrieve common elements

    List result = list1.stream()
                    .distinct()
                    .filter(list::contains)
                    .collect(Collectors.toList());
    

提交回复
热议问题