title: Remove and collect elements with Java streams
body: <p>Suppose I have a <code>Collection</code>, and a <code>Predicate</code> that matches elements I'd like to remove from the <code>Collection</code>.  But I don't just want to discard them, I want to move the matched elements into a new collection.  I'd do something like <a href="https://stackoverflow.com/a/30041628/113632">this</a> in Java 7:</p>
  
  <pre><code>List&lt;E&gt; removed = new LinkedList&lt;&gt;();
  for (Iterator&lt;E&gt; i = data.iterator(); i.hasNext();) {
      E e = i.next();
      if (predicate.test(e)) {
          removed.add(e);
          i.remove();
      }
  }
  </code></pre>
  
  <p>I'm curious if there's a streams / Java 8 way to do it.  <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate-" rel="noreferrer"><code>Collections.removeIf()</code></a> unfortunately simply returns a <code>boolean</code> (not even a count of the number of removed elements?  Too bad.)  I envision something like this (though of course <code>.removeAndYield(Predicate)</code> doesn't exist):</p>
  
  <pre><code>List&lt;E&gt; removed = data.removeAndYield(predicate).collect(Collectors.toList());
  </code></pre>
  
  <p>Note: this question was inspired by <a href="https://stackoverflow.com/q/30041206/113632">a similar question</a>; this question is about the more general case of getting a stream over the items removed from a collection.  As pointed out in the linked question, the imperative solution may be more readable, but I'm curious if this is even possible with streams.</p>
  
  <p><em>Edit:</em> Clearly we can split the task into two separate steps, and assuming the appropriate data structures it will be efficient.  The question is can this be done on arbitrary collections (which may not have efficient <code>.contains()</code> etc.).</p>