title: Java stream operation order of execution at terminal point
body: <p>I have been trying to find clear contract from official Java documentation as to in which order Java streams, once a terminal operation is called, process the elements and call intermediate operations.</p>
  
  <p>For example lets look at these examples that use both Java stream version and plain iteration version <strong>(both producing same outcome)</strong> .</p>
  
  <p><strong>Example1:</strong> </p>
  
  <pre><code>    List&lt;Integer&gt; ints = Arrays.asList(1, 2, 3, 4, 5);
      Function&lt;Integer, Integer&gt; map1 = i -&gt; i;
      Predicate&lt;Integer&gt; f1 = i -&gt; i &gt; 2;
  
      public int findFirstUsingStreams(List&lt;Integer&gt; ints){
          return ints.stream().map(map1).filter(f1).findFirst().orElse(-1);
      }
  
      public int findFirstUsingLoopV1(List&lt;Integer&gt; ints){
          for (int i : ints){
              int mappedI = map1.apply(i);
              if ( f1.test(mappedI) ) return mappedI;
          }
          return -1;
      }
  
      public int findFirstUsingLoopV2(List&lt;Integer&gt; ints){
          List&lt;Integer&gt; mappedInts = new ArrayList&lt;&gt;( ints.size() );
  
          for (int i : ints){
              int mappedI = map1.apply(i);
              mappedInts.add(mappedI);
          }
  
          for (int mappedI : mappedInts){
              if ( f1.test(mappedI) ) return mappedI;
          }
          return -1;
      }
  </code></pre>
  
  <p>Will the Java stream in <code>findFirstUsingStreams</code> method after <code>findFirst</code> is called run <code>map1</code> in the order described in <code>findFirstUsingLoopV1</code> (<code>map</code> is not run for all elements) or as described in <code>findFirstUsingLoopV2</code> (<code>map</code> is run for all elements)? </p>
  
  <p>And will that order change in future versions of Java or there is an official documentation that guarantees us the order of  <code>map1</code> calls?</p>
  
  <p><strong>Example2:</strong></p>
  
  <pre><code>Predicate&lt;Integer&gt; f1 = i -&gt; i &gt; 2;
  Predicate&lt;Integer&gt; f2 = i -&gt; i &gt; 3;
  
  
  public List&lt;Integer&gt; collectUsingStreams(List&lt;Integer&gt; ints){
      return ints.stream().filter(f1).filter(f2).collect( Collectors.toList() );
  }
  
  public List&lt;Integer&gt; collectUsingLoopV1(List&lt;Integer&gt; ints){
      List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
      for (int i : ints){
          if ( f1.test(i) &amp;&amp; f2.test(i) ) result.add(i);
      }
      return result;
  }
  
  public List&lt;Integer&gt; collectUsingLoopV2(List&lt;Integer&gt; ints){
      List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
      for (int i : ints){
          if ( f2.test(i) &amp;&amp; f1.test(i) ) result.add(i);
      }
      return result;
  }
  </code></pre>
  
  <p>Again will the Java stream in <code>collectUsingStreams</code> method after <code>collect</code> is called run <code>f1</code> and <code>f2</code> in the order described in <code>collectUsingLoopV1</code> (<code>f1</code> is evaluated before <code>f2</code>) or as described in <code>collectUsingLoopV2</code> (<code>f2</code> is evaluated before <code>f1</code>)? </p>
  
  <p>And will that order change in future versions of Java or there is an official documentation that guarantees us the order of  <code>f1</code> and <code>f2</code> calls?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Thanks for all the answers and comment but unfortunately I still dont see good explanation on the ordering of processing the elements. The docs do say that the encounter order will be preserved for lists but they dont specify how those elements will be processed. For example in case of <code>findFirst</code> the docs guarantees that <code>map1</code> will first see 1 then 2 but it does not say that <code>map1</code> wont be executed for 4 and 5. Does it mean that we cant guarantee that our order of processing will be as we expect the in fure versions of Java? Probably yes.</p>