title: Copy a stream to avoid "stream has already been operated upon or closed"
body: <p>I'd like to duplicate a Java 8 stream so that I can deal with it twice. I can <code>collect</code> as a list and get new streams from that;</p>
  
  <pre><code>// doSomething() returns a stream
  List&lt;A&gt; thing = doSomething().collect(toList());
  thing.stream()... // do stuff
  thing.stream()... // do other stuff
  </code></pre>
  
  <p>But I kind of think there should be a more efficient/elegant way.</p>
  
  <p><strong>Is there a way to copy the stream without turning it into a collection?</strong></p>
  
  <p>I'm actually working with a stream of <code>Either</code>s, so want to process the left projection one way before moving onto the right projection and dealing with that another way. Kind of like this (which, so far, I'm forced to use the <code>toList</code> trick with).</p>
  
  <pre><code>List&lt;Either&lt;Pair&lt;A, Throwable&gt;, A&gt;&gt; results = doSomething().collect(toList());
  
  Stream&lt;Pair&lt;A, Throwable&gt;&gt; failures = results.stream().flatMap(either -&gt; either.left());
  failures.forEach(failure -&gt; ... );
  
  Stream&lt;A&gt; successes = results.stream().flatMap(either -&gt; either.right());
  successes.forEach(success -&gt; ... );
  </code></pre>