title: Limit a stream and find out if there are pending elements
body: <p>I have the following code that I want to translate to Java 8 streams:</p>
  
  <pre><code>public ReleaseResult releaseReources() {
      List&lt;String&gt; releasedNames = new ArrayList&lt;&gt;();
  
      Stream&lt;SomeResource&gt; stream = this.someResources();
  
      Iterator&lt;SomeResource&gt; it = stream.iterator();
      while (it.hasNext() &amp;&amp; releasedNames.size() &lt; MAX_TO_RELEASE) {
          SomeResource resource = it.next();
          if (!resource.isTaken()) {
              resource.release();
              releasedNames.add(resource.getName());
          }
      }
  
      return new ReleaseResult(releasedNames, it.hasNext(), MAX_TO_RELEASE);
  }
  </code></pre>
  
  <p>Method <code>someResources()</code> returns a <code>Stream&lt;SomeResource&gt;</code> and <code>ReleaseResult</code> class is as follows:</p>
  
  <pre><code>public class ReleaseResult {
  
      private int releasedCount;
  
      private List&lt;String&gt; releasedNames;
  
      private boolean hasMoreItems;
  
      private int releaseLimit;
  
      public ReleaseResult(List&lt;String&gt; releasedNames, 
          boolean hasMoreItems, int releaseLimit) {
  
          this.releasedNames = releasedNames;
          this.releasedCount = releasedNames.size();
          this.hasMoreItems = hasMoreItems;
          this.releaseLimit = releaseLimit;
      }
  
      // getters &amp; setters
  }
  </code></pre>
  
  <p>My attempt so far:</p>
  
  <pre><code>public ReleaseResult releaseReources() {
  
      List&lt;String&gt; releasedNames = this.someResources()
          .filter(resource -&gt; !resource.isTaken())
          .limit(MAX_TO_RELEASE)
          .peek(SomeResource::release)
          .map(SomeResource::getName)
          .collect(Collectors.toList());
  
      return new ReleasedResult(releasedNames, ???, MAX_TO_RELEASE);
  }
  </code></pre>
  
  <p>The problem is that I can't find a way to know if there are pending resources to process. I've thought of using <code>releasedNames.size() == MAX_TO_RELEASE</code>, but this doesn't take into account the case where the stream of resources has <em>exactly</em> <code>MAX_TO_RELEASE</code> elements.</p>
  
  <p><strong>Is there a way to do the same with Java 8 streams?</strong></p>
  
  <p><strong>Note:</strong> I'm not looking for answers like <em>"you don't have to do everything with streams"</em> or <em>"using loops and iterators is fine"</em>. I'm OK if using an iterator and a loop is the only way or just the best way. It's just that I'd like to know if there's a non-murky way to do the same.</p>