我有以下案例:有一个对象列表 - ProductData包含几个字段:
public class ProductData
{
....
private String name;
private String xref;
//getters
//setters
}
Run Code Online (Sandbox Code Playgroud)
并且有API返回以下对象的列表:
public class RatingTableRow
{
private String planName;
private String planXref;
private int fromAge;
private int toAge;
private int ratingRegion;
//constructor
//getters
//setters
}
Run Code Online (Sandbox Code Playgroud)
但它返回具有空计划名称字段的对象,因为在提取此对象期间不允许这样做.我需要通过外部参照将产品数据与RatingTableRow链接,以便将计划名称设置为RatingTableRow,因为我需要稍后使用此对象,因此我创建了以下代码来执行此操作:
Map<String, ProductData> productByXref = plans.stream()
.collect(toMap(ProductData::getInternalCode, Function.identity()));
return getRatingTableRows(...).stream
.filter(ratingRow -> productByXref.containsKey(ratingRow.getPlanXref()))
.peek(row -> {
ProductData product = productByXref.get(row.getPlanXref());
row.setPlanName(product.getName());
})....;
Run Code Online (Sandbox Code Playgroud)
我知道java文档说这peek不符合这些需求,但希望得到关于如何以更正确的方式完成此任务的建议.