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

时间:2014-12-31 11:38:50

标签: java stream aggregate-functions

我有一个“请求”对象列表(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对象:(

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

1 个答案:

答案 0 :(得分:2)

不确定。您需要自定义聚合,但不需要对:

 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();