[英]Java 8 stream group by with multiple aggregation
I am trying to group my stream and aggregate on several fields我正在尝试对我的流进行分组并在多个字段上聚合
ie IE
private class Row {
private String sku;
private int colA; // sum
private int colB; // sum
private int colC; // avg
}
Example例子
sku colA colB colC
---------------------------
ar5sg5h 4 3 4
sd6ad6d 2 5 3
ar5sg5h 6 5 6
sd6ad6d 5 6 3
sd6ad6d 3 7 3
Expected:预期的:
sku colA colB colC
---------------------------
ar5sg5h 10 8 5.0
sd6ad6d 10 18 3.0
I already have List<Row> rows
, where I have intention to reduce rows with groupBy and aggregation of sum on colA and colB where as average on colC.我已经有了
List<Row> rows
,我打算在那里减少带有groupBy 的行以及 colA 和 colB 上的总和聚合,其中作为 colC 的平均值。
How can I achieve this with groupBy + multiple aggregation using Java-8 stream ?如何使用Java-8流通过groupBy +多重聚合来实现这一点?
Are you looking to:您是否正在寻找:
List<Row> result = rows.stream()
.collect(Collectors.groupingBy(Row::getSku))
.values().stream()
.map(e -> new Row(e.get(0).getSku(),
e.stream().mapToDouble(Row::getColA).sum(),
e.stream().mapToDouble(Row::getColB).sum(),
e.stream().mapToDouble(Row::getColC).average().getAsDouble()))
.collect(Collectors.toList());
Note: the type int is not helpfull in your case, I would suggest to change it to double as the average is not int
.注意: int 类型对您的情况没有帮助,我建议将其更改为 double ,因为平均值不是
int
。
Outputs输出
Row(sku=sd6ad6d, colA=10.0, colB=18.0, colC=3.0)
Row(sku=ar5sg5h, colA=10.0, colB=8.0, colC=5.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.