src/share/classes/java/util/Collection.java

Print this page
rev 6197 : [mq]: collections

@@ -23,10 +23,12 @@
  * questions.
  */
 
 package java.util;
 
+import java.util.function.Predicate;
+
 /**
  * The root interface in the <i>collection hierarchy</i>.  A collection
  * represents a group of objects, known as its <i>elements</i>.  Some
  * collections allow duplicate elements and others do not.  Some are ordered
  * and others unordered.  The JDK does not provide any <i>direct</i>

@@ -451,6 +453,30 @@
      *
      * @see Object#hashCode()
      * @see Object#equals(Object)
      */
     int hashCode();
+
+    /**
+     * Removes all of the elements of this collection which match the provided
+     * predicate.
+     *
+     * @param filter a predicate which returns {@code true} for elements to be
+     * removed
+     * @return {@code true} if any elements were removed
+     * @throws NullPointerException if the specified predicate is null
+     * @since 1.8
+     */
+    public default boolean removeAll(Predicate<? super E> filter) {
+        Objects.requireNonNull(filter);
+        boolean removed = false;
+        Iterator<E> each = iterator();
+        while (each.hasNext()) {
+            if (filter.test(each.next())) {
+                each.remove();
+                removed = true;
+            }
+        }
+
+        return removed;
+    }
 }