title: Nested for loops optimization in Java - streams alternative leads to different result
body: <p>I have tried to run in parallel 13 for loops in Java and I tried to rewrite them with DoubleStream. The problem is that the output provided by the same code, but with &quot;for&quot; loops is different than the one provided by the streams alternative.</p>
  <p>Another problem is that the &quot;for&quot; loops implementation takes a lot of time and I need to improve it. I assume the streams approach is not right because the operation that I do is serial and the order of threads is random, so the processing is not done quite right.
  Is there any way I can acquire the same result or parallelize the for loops?
  My code is below:</p>
  <pre><code>        DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(a -&gt; {
              System.out.println(&quot;Iteration &quot; + a + &quot; min var &quot; + wrapperMinVariation.minVariation + &quot;values: &quot; + wrapperMinA.minA + &quot; &quot; + wrapperMinB.minB);
              DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(b -&gt; {
                  System.out.println(&quot;Iteration &quot; + a +&quot; &quot;+ b +&quot; min var &quot; + wrapperMinVariation.minVariation + &quot;values: &quot; + wrapperMinA.minA + &quot; &quot; + wrapperMinB.minB);
                  DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(c -&gt; {
                      DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(d -&gt; {
                          DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(e -&gt; {
                              DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(f -&gt; {
                                  DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(g -&gt; {
                                      DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(h -&gt; {
                                          DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(m -&gt; {
                                              DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(n -&gt; {
                                                  DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(o -&gt; {
                                                      DoubleStream.iterate(0, x -&gt; x + 0.2).limit(5).parallel().forEach(p -&gt; {
                                                          double variation = 0;
                                                          for (int i = 0; i &lt; wrapper.list.size(); i++) {
                                                              
                                                              // operations with a,b,c,d,e,f,g,h,m,n,o,p
  
                                                              variation += variationPerMatch;
                                                          }
                                                          double finalVariationMatches = variation / wrapper.list.size();
                                                          if (finalVariationMatches &lt; wrapperMinVariation.minVariation) {
                                                              wrapperMinA.minA = a;
                                                              wrapperMinB.minB = b;
                                                              wrapperMinC.minC = c;
                                                              wrapperMinD.minD = d;
                                                              wrapperMinE.minE = e;
                                                              wrapperMinF.minF = f;
                                                              wrapperMinG.minG = g;
                                                              wrapperMinH.minH = h;
                                                              wrapperMinM.minM = m;
                                                              wrapperMinN.minN = n;
                                                              wrapperMinO.minO = o;
                                                              wrapperMinP.minP = p;
                                                              wrapperMinVariation.minVariation = finalVariationMatches;
                                                          }
                                                      });
                                                  });
                                              });
                                          });
                                      });
                                  });
                              });
                          });
                      });
                  });
              });
          });
  </code></pre>