title: Why are Java Streams once-off?
body: <p>Unlike C#'s <code>IEnumerable</code>, where an execution pipeline can be executed as many times as we want, in Java a stream can be 'iterated' only once. </p>
  
  <p>Any call to a terminal operation closes the stream, rendering it unusable.
  This 'feature' takes away a lot of power. </p>
  
  <p>I imagine the reason for this is <em>not</em> technical. What were the design considerations behind this strange restriction?</p>
  
  <p>Edit: in order to demonstrate what I am talking about, consider the following implementation of Quick-Sort in C#:</p>
  
  <pre><code>IEnumerable&lt;int&gt; QuickSort(IEnumerable&lt;int&gt; ints)
  {
    if (!ints.Any()) {
      return Enumerable.Empty&lt;int&gt;();
    }
  
    int pivot = ints.First();
  
    IEnumerable&lt;int&gt; lt = ints.Where(i =&gt; i &lt; pivot);
    IEnumerable&lt;int&gt; gt = ints.Where(i =&gt; i &gt; pivot);
  
    return QuickSort(lt).Concat(new int[] { pivot }).Concat(QuickSort(gt));
  }
  </code></pre>
  
  <p>Now to be sure, I am not advocating that this is a good implementation of quick sort! It is however great example of the expressive power of lambda expression combined with stream operation. </p>
  
  <p>And it can't be done in Java!
  I can't even ask a stream whether it is empty without rendering it unusable. </p>