title: How to get a range of items from stream using Java 8 lambda?
body: <p>In a previous question [ <a href="https://stackoverflow.com/questions/22845574/how-to-dynamically-do-filtering-in-java-8">How to dynamically do filtering in Java 8?</a> ] Stuart Marks gave a wonderful answer, and provided several useful utilities to handle selection of topN and topPercent from stream.</p>
  
  <p>I'll include them here from his original answer :</p>
  
  <pre><code>@FunctionalInterface
  public interface Criterion {
      Stream&lt;Widget&gt; apply(Stream&lt;Widget&gt; s);
  }
  
  Criterion topN(Comparator&lt;Widget&gt; cmp, long n) {
      return stream -&gt; stream.sorted(cmp).limit(n);
  }
  
  Criterion topPercent(Comparator&lt;Widget&gt; cmp, double pct) {
      return stream -&gt; {
          List&lt;Widget&gt; temp =
              stream.sorted(cmp).collect(toList());
          return temp.stream()
                     .limit((long)(temp.size() * pct));
      };
  }
  </code></pre>
  
  <p>My questions here are :</p>
  
  <p>[1] How to get top items from 3 to 7 from a stream with certain amount of items, so if the stream has items from A1, A2 .. A10, the call to</p>
  
  <pre><code>topNFromRange(Comparator&lt;Widget&gt; cmp, long from, long to) = topNFromRange(comparing(Widget::length), 3L, 7L)
  </code></pre>
  
  <p>will return { A3, A4, A5, A6, A7 }</p>
  
  <p>The simplest way I can think of is get the top 7 [ T7 ] from original, get the top 3 [ T3 ] from original, and then get T7 - T3.</p>
  
  <p>[2] How to get top items from top 10% to top 30% from a stream with certain amount of items, so if the stream has items from X1, X2 .. X100, the call to</p>
  
  <pre><code>topPercentFromRange(Comparator&lt;Widget&gt; cmp, double from, double to) = topNFromRange(comparing(Widget::length), 0.10, 0.30)
  </code></pre>
  
  <p>will return { X10, X11, X12, ..., X29, X30 }</p>
  
  <p>The simplest way I can think of is get the top 30% [ TP30 ] from original, get the top 10% [ TP10 ] from original, and then get TP30 - TP10.</p>
  
  <p>What are some better ways to use Java 8 Lambda to concisely express the above situations ?</p>