我们开始将代码升级到Java 8,这是团队中的新功能。有人告诉我做一个方法的重构,如下所示:
for (Operation so : operations) {
OperationIndex operationIndex = Transformer.getOperationIndex(so);
if (operationIndex.getServiceOperationNumber() != null) {
invoice.getOperationNumbers().add(operationIndex);
Occasion occasion = so.getOccasion().get(0);
//here's some logic using occasion and invoice
invoice.setDate(occasion.getDate());
//some more methods using occasion...
}
我正在尝试很多事情来使其发挥作用,但所有这些都失败了。毕竟我来到这个解决方案:
operations.stream()
.filter(so -> Transformer.getOperationIndex(so).getServiceOperationNumber() != null)
.forEach(so -> {
invoice.getOperationNumbers().add(Transformer.getOperationIndex(so));
Occasion occasion = so.getOccasion().get(0);
//here's some logic using occasion and invoice
invoice.setDate(occasion.getDate());
//some more methods using occasion...
});
但我仍然想知道在这种情况下它是否是最好的解决方案。这可以用更少的代码行完成吗?谢谢