英文:
Collection update optimization
问题
例如,我们在数据库中存储了一组对象(在此简化为字符串):
Collection<String> existed = someRepository.findElements(); // 返回 "A", "B", "B", "C"
我们有一个新的集合,必须替换旧数据:
Collection<String> received = Arrays.asList("B", "C", "C", "D");
最初的想法是从数据库中删除所有先前的数据,并保存来自 received
集合的所有数据,如下所示:
someRepository.removeAll(existed);
someRepository.saveAll(received);
但是,如果存在许多共同的元素,这将导致应用程序和数据库之间的多余数据传输。
另一方面,我们可以查找要删除和添加的元素,但这将需要大量的 equals
方法调用,并且在内存中检查所有项目将会更慢,而且在我看来这不是一个好方法。
最希望的结果是:
- "A",第二个 "B" 将会在要删除的集合中(我们为此调用
removeAll
) - 一个 "C","D" 将会在要添加的集合中(我们为此调用
saveAll
) - 共同的 "B" 和 "C" 将保持不变
那么,您是否有任何解决此类情况更优化的方法呢?谢谢。
英文:
For example we have collection stored objects in DB (simplified to String for example):
Collection<String> existed = someRepository.findElements(); //returns "A", "B", "B", "C"
We have a new collection that must replace old data:
Collection<String> received = Arrays.asList("B", "C", "C" ,"D");
The first idea was to remove all previous data from DB and save all from received
collection like:
someRepository.removeAll(existed);
someRepository.saveAll(received);
But in case of lot's of common elements, it will cause surplus data transfer between application and DB.
On other hand we can find elements what to remove, to add, but it requires a lot of equals
method calling and it will be slower to check all the items in memory, and IMHO its not good.
The mostly liken expected result is:
- "A", second "B" will be in toRemove collection (that we call for
removeAll
) - one "C", "D" will be in toAdd collection (that we call for
saveAll
) - common "B" and "C" will not be touched
So do you have any solution to resolve such cases more optimized?
Thanks.
答案1
得分: 0
已从Apache Commons中找到实用程序:
//获取两个集合之间的交集
Collection intersection = CollectionUtils.intersection(existed, received);
//获取已存在集合与交集之间的不相交部分
Collection toRemove = CollectionUtils.disjunction(existed, intersection);
//获取接收集合与交集之间的不相交部分
Collection toAdd = CollectionUtils.disjunction(received, intersection);
英文:
Have found utilities from Apache Commons:
Collection intersection = CollectionUtils.intersection(existed, received); //intersection between 2 collections
Collection toRemove = CollectionUtils.disjunction(existed, intersection); //getting disjunction between existed and intersection
Collection toAdd = CollectionUtils.disjunction(received, intersection); //getting disjunction between received and intersection
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论