For an example, I have a list (or set) into which the same method is to be done for each entry. This is increadibly easy with a lamba as such:
ingredientBaskets.forEach(this::processToMarmalade);
However I would like for the processToMarmalade method to return the amount of fruit pieces that could not be processed and these summed up in the end. I can do that easily by having the method sum up and return the number of errors.
What I'd like to have is basically something like:
int result = ingredientBaskets.forEach(this::processToMarmalade).sum();
some something. Or in other words something that does the same as this:
int result = 0;
for (final Basket basket: ingredientBaskets) {
result += processToMarmalade(basket)
}
}
Is there a way to do that?
EDIT: As from the answers, List and Set would allow the usage of IntStream, which is one thing that I need. What however, if it's Iterable? This one does not have a stream, just a forEach.
Assume the original question, but also for the case of Iterable. Can the forEach sum it all up someway, or do I have to create a new List and fill it up with the contents of the Iterable first?