java - 在 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
解决方案
您需要使用Collectors.summarizingDouble()
,假设您的类( 的元素类型)具有您要为其计算统计信息的Stream
某些属性:double
Map<String, DoubleSummaryStatistics>
menuStatistics =
menuPrices.parallelStream()
.collect(Collectors.groupingBy(cp -> dt1.format(cp.getUpdateDate()),
Collectors.summarizingDouble(cp -> cp.getSomeDoubleValue())));
推荐阅读
- android - Wifi Direct - 此设备地址不适用于 android 10
- react-native - 从 expo 资产中获取标题、专辑和艺术家
- c# - 在 C# 中使用 mysql 文档存储(Nosql)中的日期字段进行过滤
- c# - 属性 vs 方法,哪个更适合与数据成员交互?
- windows - 我在 Julia 中收到一条消息“使用 Plots”,但我不知道如何解决它
- r - 始终在箱线图中显示 tic 标记
- amazon-web-services - 如何在 AWS 上运行 Vue JS 应用程序?
- javascript - 模态弹出窗口不会同时运行
- firebase - Firebase Cloud Functions with Unity 使用 HTTP 可调用函数。需要帮助
- lisp - 如何修改此函数以创建动态 plist