无法使用 Stream API 编写 reduce 方法

Fail to write the reduce method using Stream API

@Getter
public class Dish {
   BigDecimal price;
}

我需要计算所有点菜的总价,但是我没有写reduce方法。 这是一个方法签名(参数有 Dish 的映射以及它被订购了多少次)。

所以一定是这样的sum of every dish.getPrice * dishQuantaty

    private BigDecimal getOrderTotalPrice(Map<Dish, Integer> dishQuantityMap) {
}

我被问到的失败代码

  return   dishQuantityMap.entrySet().stream()
        .reduce(BigDecimal.ZERO,
                (dishIntegerEntry) ->
               dishIntegerEntry.getKey().getPrice()
                        .multiply(BigDecimal.valueOf(dishIntegerEntry.getValue())));

你的意思是这样的吗:

private BigDecimal getOrderTotalPrice(Map<Dish, Integer> dishQuantityMap) {
  return dishQuantityMap.entrySet().stream()
          .map(d -> d.getKey().getPrice().multiply(new BigDecimal(d.getValue())))
          .reduce(BigDecimal.ZERO, BigDecimal::add);
}