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

Print this page
rev 8056 : 8023339: Refined Collection.removeIf UOE conditions
Reviewed-by: mduigou
Contributed-by: paul.sandoz@oracle.com
   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA


 362      * @return <tt>true</tt> if this collection changed as a result of the
 363      *         call
 364      * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
 365      *         is not supported by this collection
 366      * @throws ClassCastException if the types of one or more elements
 367      *         in this collection are incompatible with the specified
 368      *         collection
 369      *         (<a href="#optional-restrictions">optional</a>)
 370      * @throws NullPointerException if this collection contains one or more
 371      *         null elements and the specified collection does not support
 372      *         null elements
 373      *         (<a href="#optional-restrictions">optional</a>),
 374      *         or if the specified collection is null
 375      * @see #remove(Object)
 376      * @see #contains(Object)
 377      */
 378     boolean removeAll(Collection<?> c);
 379 
 380     /**
 381      * Removes all of the elements of this collection that satisfy the given
 382      * predicate.  Errors or runtime exceptions thrown by the predicate are
 383      * relayed to the caller.
 384      *
 385      * @implSpec
 386      * The default implementation traverses all elements of the collection using
 387      * its {@link #iterator}.  Each matching element is removed using
 388      * {@link Iterator#remove()}.  If the collection's iterator does not
 389      * support removal then an {@code UnsupportedOperationException} will be
 390      * thrown on the first matching element.
 391      *
 392      * @param filter a predicate which returns {@code true} for elements to be
 393      *        removed
 394      * @return {@code true} if any elements were removed
 395      * @throws NullPointerException if the specified filter is null
 396      * @throws UnsupportedOperationException if the {@code remove}
 397      *         method is not supported by this collection's
 398      *         {@link #iterator}

 399      * @since 1.8
 400      */
 401     default boolean removeIf(Predicate<? super E> filter) {
 402         Objects.requireNonNull(filter);
 403         boolean removed = false;
 404         final Iterator<E> each = iterator();
 405         while (each.hasNext()) {
 406             if (filter.test(each.next())) {
 407                 each.remove();
 408                 removed = true;
 409             }
 410         }
 411         return removed;
 412     }
 413 
 414     /**
 415      * Retains only the elements in this collection that are contained in the
 416      * specified collection (optional operation).  In other words, removes from
 417      * this collection all of its elements that are not contained in the
 418      * specified collection.


   1 f/*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA


 362      * @return <tt>true</tt> if this collection changed as a result of the
 363      *         call
 364      * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
 365      *         is not supported by this collection
 366      * @throws ClassCastException if the types of one or more elements
 367      *         in this collection are incompatible with the specified
 368      *         collection
 369      *         (<a href="#optional-restrictions">optional</a>)
 370      * @throws NullPointerException if this collection contains one or more
 371      *         null elements and the specified collection does not support
 372      *         null elements
 373      *         (<a href="#optional-restrictions">optional</a>),
 374      *         or if the specified collection is null
 375      * @see #remove(Object)
 376      * @see #contains(Object)
 377      */
 378     boolean removeAll(Collection<?> c);
 379 
 380     /**
 381      * Removes all of the elements of this collection that satisfy the given
 382      * predicate.  Errors or runtime exceptions thrown during iteration or by
 383      * the predicate are relayed to the caller.
 384      *
 385      * @implSpec
 386      * The default implementation traverses all elements of the collection using
 387      * its {@link #iterator}.  Each matching element is removed using
 388      * {@link Iterator#remove()}.  If the collection's iterator does not
 389      * support removal then an {@code UnsupportedOperationException} will be
 390      * thrown on the first matching element.
 391      *
 392      * @param filter a predicate which returns {@code true} for elements to be
 393      *        removed
 394      * @return {@code true} if any elements were removed
 395      * @throws NullPointerException if the specified filter is null
 396      * @throws UnsupportedOperationException if elements cannot be removed
 397      *         from this collection.  Implementations may throw this exception if a
 398      *         matching element cannot be removed or if, in general, removal is not
 399      *         supported.
 400      * @since 1.8
 401      */
 402     default boolean removeIf(Predicate<? super E> filter) {
 403         Objects.requireNonNull(filter);
 404         boolean removed = false;
 405         final Iterator<E> each = iterator();
 406         while (each.hasNext()) {
 407             if (filter.test(each.next())) {
 408                 each.remove();
 409                 removed = true;
 410             }
 411         }
 412         return removed;
 413     }
 414 
 415     /**
 416      * Retains only the elements in this collection that are contained in the
 417      * specified collection (optional operation).  In other words, removes from
 418      * this collection all of its elements that are not contained in the
 419      * specified collection.