public static PriceRangeData getCanonicalPriceRange(final ProductData productDataParent) {
if (MapUtils.isNotEmpty(productDataParent.getAggregates())
&& null != productDataParent.getAggregates().get(DtCoreConstants.RANGED_DATA)) {
final PriceRangeData data = new PriceRangeData();
final Map<String, Double> mapPrices = productDataParent.getRangeMapPrices();
final List<ProductData> ranges = (List<ProductData>) productDataParent.getAggregates()
.get(DtCoreConstants.RANGED_DATA);
if(MapUtils.isNotEmpty(mapPrices)) {
ranges.stream()
.map(productData -> getMapUpdatedData(mapPrices, productData));
}
final DoubleSummaryStatistics stats = ranges.stream()
.collect(Collectors.summarizingDouble(ProductData::getRawPrice));
if (null != stats) {
PriceData minPrice = new PriceData();
minPrice.setValue(new BigDecimal(stats.getMin()));
data.setMinPrice(minPrice);
PriceData maxPrice = new PriceData();
maxPrice.setValue(new BigDecimal(stats.getMax()));
data.setMaxPrice(maxPrice);
}
return data;
}
return null;
}
private static ProductData getMapUpdatedData(final Map<String, Double> mapPrices, final ProductData productData) {
if (mapPrices.containsKey(productData.getCode())) {
productData.setRawPrice(mapPrices.get(productData.getCode()));
}
return productData;
}
有没有办法结合起来
if(MapUtils.isNotEmpty(mapPrices)) {
ranges.stream()
.map(productData -> getMapUpdatedData(mapPrices, productData));
}
和
final DoubleSummaryStatistics stats = ranges.stream()
.collect(Collectors.summarizingDouble(ProductData::getRawPrice));
通过引用getRawPrice的本地方法而不是ProductData.getRawPrice。目前,我们正在将rawPrice更新为mapPrice中的那些,如果它们存在,然后重做流的统计信息。有没有办法在不必先更新的情况下执行此操作,而只需使用本地方法输入值以在summarizingDouble中计算统计数据
就像在Collectors.summarizingDouble方法中使用下面一样
private Double getRawPrice(final Map<String, Double> mapPrices, final ProductData productData) {
if (mapPrices.containsKey(productData.getCode())) {
return mapPrices.get(productData.getCode());
}
return productData.getRawPrice();
}
据我所知,你需要
ranges.stream()
.map(productData -> mapPrices.getOrDefault(productData.getCode(), productData.getRawPrice()))
.collect(Collectors.summarizingDouble(x->x));