Interesting problem. I don't think that is what Streams are intended for. Maybe like this:-...or...But please don't think either of those is a good idea.
Please explain why you would want to iterate the same List twice; it seems strange. Unless it is out of simple curiosity, “I just want to know what happens if...” That can justify almost anything in code.
By the way, that doesn't iterate the same List twice; it iterates it n× where n is equal to myList.size(). It will be interesting if you have a 1,000,000‑element List!
Campbell Ritchie wrote:Obviously that won't work at all well if any of the array elements is 0.
And what then? . . .
You can fill the resultant array with 0s and if there is only one 0 in the original array, calculate the product of all the other elements, only for that one element. If there are multiple 0s in the original array, every result will reduce to 0. I am sure OP already knows what happens if you don't compensate for 0s.
You will also get incorrect results if you suffer an arithmetic overflow calculating the product.
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad: