Simple way to compare 2 ArrayLists

后端 未结 10 1863
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 00:50

I have 2 arraylists of string object.

List sourceList = new ArrayList();
List destinationList = new ArrayList

        
10条回答
  •  误落风尘
    2020-11-30 01:12

    List oldList = Arrays.asList("a", "b", "c", "d", "e", "f");
    List modifiedList = Arrays.asList("a", "b", "c", "d", "e", "g");
    
    List added = new HashSet<>(modifiedList);
    List removed = new HashSet<>(oldList);
    
    modifiedList.stream().filter(removed::remove).forEach(added::remove);
    
    // added items
    System.out.println(added);
    // removed items
    System.out.println(removed);
    

提交回复
热议问题