title: Loop fusion of Stream in Java-8 (how it works internally)
body: <p>I'm reading the book 'Java in Action'.</p>
  
  <p>And I saw an example code of Stream in the book.</p>
  
  <pre><code>List&lt;String&gt; names = menu.stream()
                           .filter(d -&gt; {
                                         System.out.println("filtering" + d.getName());
                                         return d.getCalories() &gt; 300;
                                        })
                           .map(d -&gt; {
                                      System.out.println("mapping" + d.getName());
                                      return d.getName();
                                      })
                           .limit(3)
                           .collect(toList());
  </code></pre>
  
  <p>When the code is executed, the result is as follows.</p>
  
  <pre><code>filtering __1__.
  mapping __1__.
  filtering __2__.
  mapping __2__.
  filtering __3__.
  mapping __3__.
  </code></pre>
  
  <p>That is, because of <code>limit(3)</code>, the log message is printed only 3 times!
  In this book, this is called in "loop fusion."
  But, I don't understand this.
  Because, if you know whether an object is filtered, you have to calculate the filtering function. Then, "filtering ..." message is should be printed, I think.</p>
  
  <p>Please, Explain me about how the loop fusion works internally.</p>