4 ms·
Aggregate does the job just fine in this case. Multiple streams are not needed. .Aggregate(new PriceAndRows(), //here result is the PriceAndRows inst
by jayd16 6y ago
Aggregate does the job just fine in this case. Multiple streams are not needed.
.Aggregate(new PriceAndRows(),
//here result is the PriceAndRows instance and next is a cart row.
(result,next) =>
{
result.Price+=next.Price;
result.Rows.Add(next);
return result;
})
I'm trying to think of a better use case where multiple collectors would be cleaner than LINQ but LINQ has a lot of tools in the toolbox, SelectMany, Aggregate, temporary anonymous types, etc.
However, even in the Java side, the example could be done with reduce alone, I think.
- kaba0 6y ago> However, even in the Java side, the example could be done with reduce alone, I think. That’s my point: teeing itself is not aggregate/reduce. If after the branching the streams differ in size, reduce no longer applies, for example.
- jayd16 6y agoBut my point is that Aggregate and reduce can handle that in this case because they both can simply add to the PriceAndRows instance incrementally. You can sum the total and build the list in the body of the Aggregate/reduce method. Teeing is pointless here. There's no need to use another collector.