要在没有自定义收集器的情况下执行此操作(不再对结果进行流式传输),您可以这样做.它有点脏,因为它首先收集到Map< String,List< TimePeriodCalc>>然后流式传输该列表并获得平均加倍.
由于你需要两个平均值,它们被收集到一个Holder或一对,在这种情况下我使用的是AbstractMap.SimpleEntry
Map> map = Stream.of(new TimePeriodCalc(12d, 10d, "A"), new TimePeriodCalc(2d, 16d, "A"))
.collect(Collectors.groupingBy(TimePeriodCalc::getAtDate,
Collectors.collectingAndThen(Collectors.toList(), list -> {
double occupancy = list.stream().collect(
Collectors.averagingDouble(TimePeriodCalc::getOccupancy));
double efficiency = list.stream().collect(
Collectors.averagingDouble(TimePeriodCalc::getEfficiency));
return new AbstractMap.SimpleEntry<>(occupancy, efficiency);
})));
System.out.println(map);