猿问

在 Java8 中收集统计信息

我想在对一组数据进行分组后收集统计信息,但我不知道是否可以


Map<String, DoubleSummaryStatistics> menuStatistics =


                menuPrices.parallelStream()

                        .collect(Collectors.groupingBy(cp -> dt1.format(cp.getUpdateDate())),

                                Collector.of(DoubleSummaryStatistics::new));

因为我有一些编译问题:


Multiple markers at this line

    - The method of(Supplier<R>, BiConsumer<R,T>, BinaryOperator<R>, Collector.Characteristics...) in the type Collector is not applicable for the arguments 

     (DoubleSummaryStatistics::new)

    - The constructed object of type DoubleSummaryStatistics is incompatible with the descriptor's return type: R



蛊毒传说
浏览 152回答 1
1回答

莫回无

您需要使用Collectors.summarizingDouble(),假设您的类( 的元素类型Stream)有一些double您想要计算统计数据的属性:Map<String, DoubleSummaryStatistics>&nbsp; &nbsp; menuStatistics =&nbsp; &nbsp; &nbsp; &nbsp; menuPrices.parallelStream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(cp -> dt1.format(cp.getUpdateDate()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collectors.summarizingDouble(cp -> cp.getSomeDoubleValue())));
随时随地看视频慕课网APP

相关分类

Java
我要回答