如何在流中合并统计信息和映射器?

时间:2019-04-08 08:08:07

标签: java java-8 java-stream collectors

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中的rawPrice(如果存在),然后重做流以获取统计信息。有没有一种方法而不必先进行更新,而只是使用local方法输入值来计算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();
    }

1 个答案:

答案 0 :(得分:1)

据我了解,您需要

const path = require('path')

module.exports = {
  presets: [],
  plugins: [],
  exclude: [
    path.resolve('file path')
  ]
}