如何使用Java流计算两个聚合函数?

我有一个像这样的“请求”对象列表(id,金额,价格)

List<Request> requests = Arrays.asList(
        new Request(id++, 20, 59.28),
        new Request(id++, 10, 61.23),
        new Request(id++, 30, 60.67),
        new Request(id++, 25, 60.16),
        new Request(id++, 60, 59.67));

我想在一次迭代中计算两个指标 – 总和(金额)和总和(金额*价格).我需要它们来计算平均价格:总和(金额*价格)/总和(金额).

考虑到我想使用Java 8流,我发现的唯一变体是将值映射到Pair对象并实现自定义使用者:

static class Aggregate implements Consumer<Pair<Double, Double>> {
    private double count = 0L;
    private double sum = 0L;

    public double average() {
        return count > 0 ? sum/(double) count : 0;
    }

    public void combine(Aggregate other) {
        count += other.count;
        sum += other.sum;
    }

    @Override
    public void accept(Pair<Double, Double> data) {
        this.count += data.getLeft();
        this.sum += data.getLeft() * data.getRight();
    }
}

Double avgPrice = requests.stream()
        .map(e -> Pair.<Double, Double>of(e.getAmount(), e.getPrice()))
        .collect(Aggregate::new, Aggregate::accept, Aggregate::combine)
        .average();

这种方法看起来很混乱 – 我们必须为每个条目创建额外的Pair对象:(

有谁知道更好的解决方案?

最佳答案 当然.您需要自定义聚合,但不需要对:

 static class Aggregate {
   private long count = 0L;
   private double sum = 0L;
   double average() { return sum / count; }
   void merge(Aggregate other) {
     count += other.count;
     sum += other.sum;
   }
   void add(int count, double value) {
     this.count += count;
     this.sum += count * value;
   }
 }
}

requests.stream().collect(
   Aggregate::new,
   (aggr, request) -> aggr.add(request.getCount(), request.getPrice()),
   Aggregate::merge)
 .average();

而且您实际上并不需要实现Consumer.

老实说,多通道解决方案可能非常快,也更简单……

requests.stream()
      .mapToDouble(request -> request.getCount() * request.getPrice())
      .sum()
   / requests.stream().mapToLong(Request::getCount).sum();
点赞