新数据:
ArrayList<Comment> newComments = getComments();
已有数据,已填写一些数据:
ArrayList<Comment> oldComments
我无法用新内容覆盖整个旧内容。
我想要新的(尚未存在)Comment
newComments
中的对象添加到oldComments
,我也想要 newComments
中缺失的那些将从 oldComments
中删除。
Collections
有什么好的方法吗?包这个?或者任何其他方式?
注:Comment
类有自定义compare()
方法(不使用默认 equals()
)
最佳答案
java.util.Set
更适合您的用例。然后你可以这样做:
oldComments.retainAll(newComments);
oldComments.addAll(newComments);
关于java - 一个 ArrayList 与另一个 ArrayList 的差异同步(添加、删除但不覆盖),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164421/