java 比较两个列表的更新、删除和添加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1667481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two lists for updates, deletions and additions
提问by Pablojim
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).
我有一个新列表和一个旧列表。在 Java 中,是否有一种标准方式/库可以让我比较这两个列表并确定哪些项目已更新/删除或全新?例如,我应该最终得到三个列表 - 已删除的项目(旧项目但不是新项目)、更新项目(两者都有项目)、新项目(新项目和旧项目)。
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.
列表中的对象实现正确等于。
回答by cletus
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:
没有标准的方式抱歉。但是,您可以使用标准 JDK 相当轻松地完成此操作,而无需添加对 Apache Commons 的依赖(正如其他人所建议的那样)。假设您的列表是List<T>实例:
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);
回答by Andrzej Doyle
There's nothing in the standard libraries.
标准库中没有任何内容。
However the Apache Commons CollectionUtilsclass gives you this functionality with the intersection and subtract methods:
但是,Apache Commons CollectionUtils类通过交集和减法方法为您提供了此功能:
Collection<T> old = ...;
Collection<T> neww = ...;
Collection<T> deleted = (Collection<T>)CollectionUtils.subtract(old, new);
Collection<T> updated = (Collection<T>)CollectionUtils.intersection(old, new);
Collection<T> newResult = (Collection<T>)CollectionUtils.subtract(new, old);
(You need the (unchecked) casts because CollectionUtils isn't generified.)
(您需要(未选中的)强制转换,因为 CollectionUtils 未泛化。)
回答by I82Much
I would use Apache CollectionUtilsand use the union (items in both) and disjunction functions (change the order to get one or the other).
我会使用Apache CollectionUtils并使用联合(两者中的项目)和分离函数(更改顺序以获取一个或另一个)。
Ideally you'd make one pass over all the elements instead of 3, but if this isn't your bottleneck, I wouldn't worry about efficiency right now.
理想情况下,您将遍历所有元素而不是 3 个,但如果这不是您的瓶颈,我现在不会担心效率。
回答by Kevin Bourrillion
Personally, I believe that the only sensible way to explain the difference between two listsis with a full-blown diff algorithm (like that of the unix diff command).
就个人而言,我认为解释两个列表之间差异的唯一合理方法是使用成熟的 diff 算法(如 unix diff 命令的算法)。
Sets, though, are a much simpler story. Google Collections provides a Sets.difference(Set, Set)method, as well as union and intersection.
然而,Sets是一个简单得多的故事。Google Collections 提供了Sets.difference(Set, Set)方法,以及并集和交集。
回答by NickDK
I think you can accomplish this with the standard java library too. Take a look at the following methods of java.util.Collection:
我认为您也可以使用标准 Java 库来完成此操作。看看java.util.Collection的以下方法:
retainAll(Collection c)
保留所有(集合 c)
Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
仅保留此集合中包含在指定集合中的元素(可选操作)。换句话说,从该集合中删除所有未包含在指定集合中的元素。
removeAll(Collection c)
删除所有(集合 c)
Removes all this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
删除也包含在指定集合中的所有此集合的元素(可选操作)。此调用返回后,此集合将不包含与指定集合相同的元素。
回答by PhiLho
If there is a standard way, I don't know it...
I looked at Collectionsbut only saw disjoint() (that's already an information...) and indexOfSubList() (not sure if it is useful at all).
I also looked at Google Collectionsand if there is, apparently, not such facility, there are some useful tools there, like Collections2's filter() function which can help if you make a proper Predicate.
如果有一个标准的方式,我不知道它......
我查看了Collections但只看到了 disjoint() (这已经是一个信息......)和 indexOfSubList() (不确定它是否有用)。
我还查看了Google Collections,如果显然没有这样的工具,那里有一些有用的工具,例如Collections2的 filter() 函数,如果您做出正确的谓词,它会有所帮助。
[EDIT] I missed the removeAll and retainAll methods of Collection... I don't delete this answer, even if a bit pathetic, as it is somehow complementary of the other answers... (I think Google Collections is at least worth mentioning!)
[编辑] 我错过了 Collection 的 removeAll 和 retainAll 方法......我不会删除这个答案,即使有点可怜,因为它在某种程度上是其他答案的补充......(我认为谷歌收藏至少值得提到!)

