title: Stream<Stream>: flatMap vs. reduce
body: <p>If I execute the following code which "concatenates" two streams </p>
  
  <ul>
  <li>first by flatMapping a <code>Stream&lt;Stream&lt;Integer&gt;&gt;</code></li>
  <li>then by reducing a <code>Stream&lt;Stream&lt;Integer&gt;&gt;</code> using <code>Stream.concat()</code></li>
  </ul>
  
  <p>I obtain the same correct result in both cases, but the number of filtering operations is different.</p>
  
  <pre><code>public class FlatMapVsReduce {
      public static void main(String[] args) {
          List&lt;Integer&gt; list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
  
          Predicate&lt;Integer&gt; predicate1 = i -&gt; {
              System.out.println("testing first condition with " + i);
              return i == 3;
          };
  
          Predicate&lt;Integer&gt; predicate2 = i -&gt; {
              System.out.println("testing second condition with " + i);
              return i == 7;
          };
  
          System.out.println("Testing with flatMap");
          Integer result1 =
              Stream.of(list.stream().filter(predicate1),
                        list.stream().filter(predicate2))
                    .flatMap(Function.identity())
                    .peek(i -&gt; System.out.println("peeking " + i))
                    .findFirst()
                    .orElse(null);
          System.out.println("result1 = " + result1);
  
          System.out.println();
          System.out.println("Testing with reduce");
          Integer result2 =
              Stream.of(list.stream().filter(predicate1),
                        list.stream().filter(predicate2))
                    .reduce(Stream::concat)
                    .orElseGet(Stream::empty)
                    .peek(i -&gt; System.out.println("peeking " + i))
                    .findFirst()
                    .orElse(null);
          System.out.println("result2 = " + result2);
      }
  }
  </code></pre>
  
  <p>I get the expected result in both cases (3). However, the first operation applies the first filter on every element of the collection, whereas the second stops as soon as one is met. The output is:</p>
  
  <pre><code>Testing with flatMap
  testing first condition with 1
  testing first condition with 2
  testing first condition with 3
  peeking 3
  testing first condition with 4
  testing first condition with 5
  testing first condition with 6
  testing first condition with 7
  testing first condition with 8
  testing first condition with 9
  result1 = 3
  
  Testing with reduce
  testing first condition with 1
  testing first condition with 2
  testing first condition with 3
  peeking 3
  result2 = 3
  </code></pre>
  
  <p>Why is there a difference of behavior between the two? Could the JDK code be improved to be as efficient in the first scenario than in the second one, or is there something in flatMap that makes it impossible?</p>
  
  <p>Addendum: the following alternative is as efficient as the one using reduce, but I still can't explain why:</p>
  
  <pre><code>    Integer result3 = Stream.of(predicate1, predicate2)
                              .flatMap(c -&gt; list.stream().filter(c).limit(1))
                              .peek(i -&gt; System.out.println("peeking " + i))
                              .findFirst()
                              .orElse(null);
      System.out.println("result3 = " + result3);
  </code></pre>