【发布时间】:2021-04-14 05:40:00
【问题描述】:
我有以下方法:
public String getAllDangerousProductsName(Offer offer){
return offer.getOfferRows().stream()
.filter(row -> row.isDangerousGood())
.map(row -> row.getItemInformation().getOfferTexts().getName())
.collect(Collectors.joining(","));
}
我想将此方法用于 row.isBulkyGood()。我目前正在做的是
public String getAllBulkyProductsName(Offer offer){
return offer.getOfferRows().stream()
.filter(row -> row.isBulkyGood())
.map(row -> row.getItemInformation().getOfferTexts().getName())
.collect(Collectors.joining(","));
}
... 这基本上是代码重复。有没有办法可以将函数作为方法参数传递来优化它,以便为两种过滤条件提供一种方法?
【问题讨论】:
标签: java lambda java-8 java-stream