Averaging across multiple fields with IntSummaryStatistics

前端 未结 2 656
无人共我
无人共我 2021-01-07 01:07

I\'m trying to use Java 8 streams to create a single CarData object, which consists of an average of all the CarData fields in the list coming from getCars;

2条回答
  •  情深已故
    2021-01-07 01:19

    Starting with JDK 12, you can use the following solution:

    CarData average = carData.stream().collect(Collectors.teeing(
        Collectors.averagingInt(CarData::getBodyWeight),
        Collectors.averagingInt(CarData::getShellWeight),
        (avgBody, avgShell) -> new CarData(avgBody.intValue(), avgShell.intValue())));
    

    For older Java versions, you can do either, add the teeing implementation of this answer to your code base and use it exactly as above or create a custom collector tailored to your task, as shown in Andreas’ answer.

    Or consider that streaming twice over a List in memory is not necessarily worse than doing two operations in one stream, both, readability- and performance-wise.

    Note that calling intValue() on Double objects has the same behavior as the (int) casts in Andreas’ answer. So in either case, you have to adjust the code if other rounding behavior is intended.

    Or you consider using a different result object, capable of holding two floating point values for the averages.

提交回复
热议问题