The problem is complex, but it comes down to one thing, it knows your data. Is it imutables, objects with id, duplicate entries, etc.? The code below works for immutable values โโwith single values โโ(and with possible duplicates). First, he tries to delete all entries in the previous list (from the copied list after the list). It remains to add the added elements. Those from the list that can be removed from the list are immutable. The rest are deleted.
package scrap; import java.util.ArrayList; import java.util.List; public class ListDiffer<T> { private List<T> addedList = new ArrayList<>(); private List<T> unchangedList = new ArrayList<>(); private List<T> removedList = new ArrayList<>(); public ListDiffer(List<T> beforeList, List<T> afterList) { addedList.addAll(afterList);
source share