title: performance of Stream.sorted().limit()
body: <p>Java Streams sport both <code>sorted</code> and <code>limit</code> methods, which respectively return a sorted version of a stream and return a stream just returning a specified number of items of a stream. When these operations are applied in succession, such as in:</p>
  
  <pre><code>stream.sorted().limit(qty).collect(Collectors.toList())
  </code></pre>
  
  <p>is the sorting is performed in a way that sorts <code>qty</code> items or is the entire list sorted? In other words, if <code>qty</code> is fixed, is this operation in <code>O(n)</code>? The documentation doesn't specify the performance of these methods alone or in conjunction with each other.</p>
  
  <p>The reason I ask is that the obvious imperative implementation of these operations would be to sort and then limit, taking time <code>Θ(n * log(n))</code>. But these operations together can be performed in <code>O(n * log(qty))</code> and a smart streaming framework could view the entire stream before executing it to optimize this special case.</p>