title: Is Java 8 stream laziness useless in practice?
body: <p>I have read a lot about Java 8 streams lately, and several articles about lazy loading with Java 8 streams specifically: <a href="http://java.amitph.com/2014/01/java-8-streams-api-laziness.html" rel="noreferrer">here</a> and <a href="https://www.logicbig.com/tutorials/core-java-tutorial/java-util-stream/lazy-evaluation.html" rel="noreferrer">over here</a>. I can't seem to shake the feeling that lazy loading is COMPLETELY useless (or at best, a minor syntactic convenience offering zero performance value).</p>
  
  <p>Let's take this code as an example:</p>
  
  <pre><code>int[] myInts = new int[]{1,2,3,5,8,13,21};
  
  IntStream myIntStream = IntStream.of(myInts);
  
  int[] myChangedArray = myIntStream
                          .peek(n -&gt; System.out.println("About to square: " + n))
                          .map(n -&gt; (int)Math.pow(n, 2))
                          .peek(n -&gt; System.out.println("Done squaring, result: " + n))
                          .toArray();
  </code></pre>
  
  <p>This will log in the console, because the <code>terminal operation</code>, in this case <code>toArray()</code>, is called, and our stream is lazy and executes only when the terminal operation is called. Of course I can also do this:</p>
  
  <pre><code>  IntStream myChangedInts = myIntStream
      .peek(n -&gt; System.out.println("About to square: " + n))
      .map(n -&gt; (int)Math.pow(n, 2))
      .peek(n -&gt; System.out.println("Done squaring, result: " + n));
  </code></pre>
  
  <p>And nothing will be printed, because the map isn't happening, because I don't need the data. Until I call this:</p>
  
  <pre><code>  int[] myChangedArray = myChangedInts.toArray();
  </code></pre>
  
  <p>And voila, I get my mapped data, and my console logs. Except I see zero benefit to it whatsoever. I realize I can <em>define</em> the filter code long before I call to <code>toArray()</code>, and I can pass around this "not-really-filtered stream around), but so what? Is this the only benefit?</p>
  
  <p>The articles seem to imply there is a performance gain associated with laziness, for example:</p>
  
  <blockquote>
    <p>In the Java 8 Streams API, the intermediate operations are lazy and their internal processing model is optimized to make it being capable of processing the large amount of data with high performance.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>Java 8 Streams API optimizes stream processing with the help of short circuiting  operations. Short Circuit methods ends the stream processing as soon as their conditions are satisfied. In normal words short circuit operations, once the condition is satisfied just breaks all of the intermediate operations, lying before in the pipeline. Some of the intermediate as well as terminal operations have this behavior.</p>
  </blockquote>
  
  <p>It sounds literally like breaking out of a loop, and not associated with laziness at all.</p>
  
  <p>Finally, there is this perplexing line in the second article:</p>
  
  <blockquote>
    <p>Lazy operations achieve efficiency. It is a way not to work on stale data. Lazy operations might be useful in the situations where input data is consumed gradually rather than having whole complete set of elements beforehand. For example consider the situations where an infinite stream has been created using Stream#generate(Supplier&lt;T>) and the provided Supplier function is gradually receiving data from a remote server. In those kind of the situations server call will only be made at a terminal operation when it's needed.</p>
  </blockquote>
  
  <p>Not working on stale data? What? How does lazy loading keep someone from working on stale data?</p>
  
  <hr>
  
  <p>TLDR: Is there any benefit to lazy loading besides being able to run the filter/map/reduce/whatever operation at a later time (which offers zero performance benefit)?</p>
  
  <p>If so, what's a real-world use case?</p>