title: How to use streams with interfering methods and constructors, and why not use .peek()?
body: <p>I'll show a simplified example of my problem to help demonstrate it.</p>
  
  <p>let's say I'm trying to convert an old java code to java 8 code for style and parallelization purposes (Which i do).</p>
  
  <p>this is the code:</p>
  
  <pre><code>    public static boolean deleteTester(List&lt;Integer&gt; keys) {
          DHeap dHeap = new DHeap(d, keys.size());
          DHeap_Item[] DHeap_ItemArray = keysListToDHeap_ItemArray(keys);
          dHeap.arrayToHeap(DHeap_ItemArray);
  
          for (int i = 0; i &lt; keys.size(); i++) {
              keys.set(i, null);
              dHeap.delete(DHeap_ItemArray[i]);
              if (!someTest(keys, dHeap.getList()))
                  return false;
          }
          return true;
      }
  </code></pre>
  
  <p>There are several issues in the conversion:</p>
  
  <ol>
  <li>I need to check every iteration, so i can't reduce/collect and then check.</li>
  <li>I am accessing an array and a data structure object (which the tester is for), meaning it isn't non-interfering, and isn't  stateless operation.</li>
  </ol>
  
  <p>Here is my attempt at replacing the for loop:</p>
  
  <pre><code>    return IntStream.range(0, keys.size())
              //.parallel()
              .peek(idx -&gt; keys.set(idx, null))
              .peek(idx -&gt; dHeap.delete(DHeap_ItemArray[idx]))
              .allMatch(e -&gt; someTest(keys, dHeap.getList()));
  </code></pre>
  
  <p>which is much shorter and readable, however "breaks the rules" and in result unable to parallel.</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>Why am I unable to use <code>.parallal()</code>? (I get a null pointer ex.)</li>
  <li>Why shouldn't we use peek()? because in this code i see no other way around it.</li>
  <li>What should I do when accessing outside elements?</li>
  <li>How should I "transfer" the code to java 8 streams?</li>
  <li>What am I missing?</li>
  </ol>
  
  <p>I guess that the answers are intertwined.
  I just started to learn java 8 so thanks a lot for all the help because I'm really stuck.</p>