I believe I can do next using one stream operation on listOfPricedObjects:
List<BigDecimal> myList = new ArrayList();
myList = listOfPricedObjects.stream().map(PricedObject::getPrice).collect(Collectors.toList());
BigDecimal sum = listOfPricedObjects.stream().map(PricedObject::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add)
How I can fill myList with prices and calculate sum of prices using stream one time? Thanks
UPD: As the result I need myList filled with prices and sum variable with sum. But not with usding stream() twice for that.
What you want here is to collect your elements inside 2 collectors: the first one would collect into a list, and the second one would sum the price.
Since there are no such collectors in the Stream API itself, we can easily construct our own. Let's create a class ResultHolder
that will hold the result of the Stream pipeline: this is the list of decimals and the sum.
class ResultHolder {
List<BigDecimal> list = new ArrayList<>();
BigDecimal sum = BigDecimal.ZERO;
}
Finally, we can use it with:
ResultHolder resultHolder =
listOfPricedObjects.stream()
.map(PricedObject::getPrice)
.collect(
ResultHolder::new,
(r, p) -> { r.list.add(p); r.sum = r.sum.add(p); },
(r1, r2) -> { r1.list.addAll(r2.list); r1.sum = r1.sum.add(r2.sum); }
);
System.out.println(resultHolder.list);
System.out.println(resultHolder.sum);
This will work in a parallel pipeline and will keep the initial order of the list, contrary to the other answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With