我正在尝试重写这个复杂的double-for循环,这似乎有点贵,我认为使用流重写它可以提高可读性,甚至可以避免不必要的循环迭代。
我想在这里转换这个命令式代码:
public Documents clearDocuments(List<Document> toDeleteDocs) {
Documents softDeleteList = new Documents();
List<EntityKeyVersion> deleteList = entityManager.bulkDeleteEntities(ekList,EntityClient.DeleteMode.SOFT_DELETE);
for (Document doc : toDeleteDocs) {
for (EntityKeyVersion ekv : deleteList) {
if (ekv.getEntityKey().contentEquals(doc.getEntityKey())) {
doc.setVersion((ekv.getEntityVersion()));
softDeleteList.getDocument().add(doc);
} else if (softDeleteList.getDocument().size() == deleteList.size()) {
break; // once soft delete list is fully populated, break out of loop to save unnecessary iterations
}
}
}
return softDeleteList;
}
要利用Java8流,
这是我迄今为止尝试的,我认为我有双循环下降,但内部条件作为过滤器可能是错误的,似乎我错过了不少:
Documents softDeleteList = new Documents();
List<EntityKeyVersion> deleteList = entityManager.bulkDeleteEntities(ekList,EntityClient.DeleteMode.SOFT_DELETE);
return softDeleteList.getDocument().stream()
.filter(doc -> deleteList.stream()
.peek(jh -> )
.anyMatch(ekv -> ekv.getEntityKey().contentEquals(doc.getEntityKey())))
.collect(Collectors.toList());
如果您有任何帮助或帮助,我们将不胜感激,谢谢
暂无答案!
目前还没有任何答案,快来回答吧!