• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Nested for loop as a stream

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I iterate over the same list twice, with a stream.Something like a nested for loop.


 
Marshal
Posts: 73334
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Fernando Stark
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to do the following method as a stream.

 
Campbell Ritchie
Marshal
Posts: 73334
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do it in one loop when multiple stages would be quicker?Obviously that won't work at all well if any of the array elements is 0.
 
Saloon Keeper
Posts: 4560
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Obviously that won't work at all well if any of the array elements is 0.


And what then?    

Here is a Stream version of OP's code:

If that is shorter/easier? Hmm...
 
Campbell Ritchie
Marshal
Posts: 73334
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

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:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic