title: The use of .peek() in java 8 streams
body: <p>I am using .peek() in my stream but it is frowned upon, sadly I can't find a solution.</p>
  
  <p>Simplified version:</p>
  
  <pre><code>static boolean fooAddTester(int size) {
      Foo foo = new foo(); //data structure
  
      return IntStream.range(0, size).
              .peek(i -&gt; synchronized(r){foo.add(i)})
              .allMatch(e -&gt; foo.isLegal());
  }
  </code></pre>
  
  <p>what I need to do is to iterate over the IntStream and to check <strong>after every insert</strong> if the foo data structure is legal.
  This is logically equivalent to:</p>
  
  <pre><code>static boolean fooAddTester(int size) {
      Foo foo = new foo(); //data structure
  
      for(int i=0; i&lt;size; i++){
          foo.add(i);
          if(!foo.isLegal())
              return false;
      return true;
  }
  </code></pre>
  
  <p>However it is more complex and I'm trying to use streams to simplify and learn.</p>
  
  <p>A way to do the same without using <code>.peek()</code> is this: which does work - but I'm just "moving" the problem to <code>.allMatch()</code>:</p>
  
  <pre><code>return IntStream.range(0, size).
              .allMatch(i -&gt; {
                   synchronized(r){foo.add(i)};
                   foo.isLegal();
               )};
  </code></pre>
  
  <p>My problem is very similar to <a href="https://stackoverflow.com/questions/33635717/in-java-streams-is-peek-really-only-for-debugging">this question</a>, the diffrence is that I am checking every time so the solutions aren't working.</p>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>Is <code>.peek()</code> really only to debugging or can I use it in this manner?</li>
  <li>Is there any better solution?</li>
  <li>Should I use my second solution?</li>
  </ul>
  
  <p>I'm looking for a correct solution, not a working solution, all of those codes are already working.</p>