我有一个"请求"对象(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();