How can I return the difference between two lists?

后端 未结 10 681
终归单人心
终归单人心 2020-11-27 03:49

I have two array lists e.g.

List a;
contains : 10/10/2014, 10/11/2016

List b;
contains : 10/10/2016

How can i do

10条回答
  •  长情又很酷
    2020-11-27 04:27

    You can use filter in the Java 8 Stream library

    List aList = List.of("l","e","t","'","s");
    List bList = List.of("g","o","e","s","t");
    
    List difference = aList.stream()
        .filter(aObject -> {
            return ! bList.contains(aObject);
          })
        .collect(Collectors.toList());
    
    //more reduced: no curly braces, no return
    List difference2 = aList.stream()
        .filter(aObject -> ! bList.contains(aObject))
        .collect(Collectors.toList());
    

    Result of System.out.println(difference);:

    [e, t, s]

提交回复
热议问题