title: Is flatMap an efficient way to add elements to a stream
body: <p>For example, see the following function which duplicates even numbers in a stream:</p>
  
  <pre><code>Stream&lt;Integer&gt; duplicateEvens(Stream&lt;Integer&gt; items) {
      return items.flatMap(i -&gt; {
          if (i % 2 == 0) {
              return Stream.of(i, i);
          } else {
              return Stream.of(i);
          }
      }
  }
  </code></pre>
  
  <p>An alternative approach could be to use a stream builder as follows:</p>
  
  <pre><code>Stream&lt;Integer&gt; duplicateEvens(Stream&lt;Integer&gt; items) {
  Stream.Builder result = builder();
  Stream&lt;Integer&gt; extra = items.peek(result.add(i))
  .filter(i -&gt; i%2 == 0);
  return Stream.concat(result.build(), extra);
  }
  </code></pre>