Nick P Nick P - 2 years ago 105
Java Question

Java Stream- Add two columns and sum results

I have a Price object that has two attributes, cost and tax. I want to be able to stream over a list of Price Objects, add the cost and tax together, and then sum up the total.

Is there a way to do this entirely in a Java 8 stream? I am thinking of logic along the lines of this:

List<Price> prices = Price.find.all();
Double sum = 0.0; -> { sum = sum + price.cost + });

Answer Source
double total =
                     .mapToDouble(price -> price.cost +

As simple as that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download