我有一个List<SqlResult>
. 我需要List<Result>
从 SqlResult 的聚合中得到一个。例如,
SqlResult(输入)
public class SqlResult{
private String key1;
private String key2;
private Double val;
// getters, setters and constructor
结果数据类
public class Result{
private String key1;
private String key2;
private Double avgVal;
private Long count;
// getters, setters and constructor
我想得到下面的列表,用key1、key2、成员计数(count)、val(avgVal)的平均值分组下面的代码抛出NullPointerException。
public class Main
{
public static void main(String[] args) {
List<SqlResult> listSqlResult = new ArrayList<>();
listSqlResult.add(new SqlResult(a1,b1,123));
listSqlResult.add(new SqlResult(a1,b1,10));
listSqlResult.add(new SqlResult(a1,b1,23));
listSqlResult.add(new SqlResult(a1,b2,3));
listSqlResult.add(new SqlResult(a1,b2,73));
listSqlResult.add(new SqlResult(a1,b2,15));
listSqlResult.add(new SqlResult(a2,b1,43));
listSqlResult.add(new SqlResult(a2,b1,19));
listSqlResult.add(new SqlResult(a2,b1,15));
listSqlResult.add(new SqlResult(a2,b2,38));
listSqlResult.add(new SqlResult(a2,b2,73));
listSqlResult.add(new SqlResult(a2,b2,15));
List<Result> listResult = listSqlResult.stream
.collect(groupingBy(SqlResult::getKey1,
groupingBy(SqlResult::getKey2))).values().stream()
.map(e -> new Result(e.get(0).get(0).getKey1(), e.get(0).get(0).getKey2(),e.get(0).stream().mapToDouble(e::getValue).average(), e.get(0).stream().count()))
.collect(Collectors.toList())
;
如何使用 Java Stream API 获得统计结果(val 平均值、成员计数)?