public Double getUnitsBefore(Date recordDate) {
    double eligibleUnits = 0;
    ageBucket.forEach((dateStr, units) -> {
        try {
            Date date = DFT.parse(dateStr);
            if (date.before(recordDate)) {
                //TODO These are eligible units for dividend.
                eligibleUnits = eligibleUnits + units.doubleValue();    //TODO
            }

        } catch(ParseException e) {
            e.printStackTrace();
        }
    });

    return eligibleUnits;
}



如您所知,eligibleUnits = eligibleUnits + units.doubleValue();行无法在Java 8中编译。我如何实现这一点?我只需要在日期早于记录日期时求和。

最佳答案

改用流:

return ageBucket.entrySet()
        .stream()
        .filter(e -> DFT.parse(e.getKey()).before(recordDate))
        .mapToDouble(Map.Entry::getValue)
        .sum();

10-04 18:21