In Java 8, if I have a list of objects like this:
MyObject double a; double b; double c;
I want to get the results of each of these fields in the list of objects. One way to do this:
double totalA = myListOfObjects.stream().map(e -> e.getA()).reduce(0.0, (x, y) -> x + y); double totalB = myListOfObjects.stream().map(e -> e.getB()).reduce(0.0, (x, y) -> x + y); double totalC = myListOfObjects.stream().map(e -> e.getC()).reduce(0.0, (x, y) -> x + y);
But is there any way to combine this in one pass through a list of objects that use api threads? If I just wrote a for / while loop (see below) and manually added 3 total values โโthat would look more efficient than the above 3 lines of code)
for (MyObject obj: myListOfObjects) { totalA += obj.getA(); totalB += obj.getB(); totalC += obj.getC(); }
thanks
source share