Simple question.
I have a new list and an old list. In Java is there a standard way/library that allows me to compare these two lists and determine which items have been updated/deleted or are completely new? E.g. I should end up with three lists - Deleted items (items in old but not in new), Updated items (items in both), New items (items in new and not in old).
I could write this myself but was wondering if there is a standard way to do it.
The objects in the list implement equals correctly.
No standard way sorry. You can do it fairly easily with the standard JDK without resorting to adding a dependency on Apache Commons (as others have suggested) however. Assuming your lists are List<T>
instances:
List<T> oldList = ...
List<T> newList= ...
List<T> removed = new ArrayList<T>(oldList);
removed.removeAll(newList);
List<T> same = new ArrayList<T>(oldList);
same.retainAll(newList);
List<T> added = new ArrayList<T>(newList);
added.removeAll(oldList);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With