< prev index next >

src/java.base/share/classes/java/util/AbstractCollection.java

Print this page
rev 54827 : 6394757: AbstractSet.removeAll semantics are surprisingly dependent on relative sizes
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 1997, 2018, 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
  22  * or visit www.oracle.com if you need additional information or have any


 299                     it.remove();
 300                     return true;
 301                 }
 302             }
 303         }
 304         return false;
 305     }
 306 
 307 
 308     // Bulk Operations
 309 
 310     /**
 311      * {@inheritDoc}
 312      *
 313      * @implSpec
 314      * This implementation iterates over the specified collection,
 315      * checking each element returned by the iterator in turn to see
 316      * if it's contained in this collection.  If all elements are so
 317      * contained {@code true} is returned, otherwise {@code false}.
 318      *



 319      * @throws ClassCastException            {@inheritDoc}
 320      * @throws NullPointerException          {@inheritDoc}
 321      * @see #contains(Object)
 322      */
 323     public boolean containsAll(Collection<?> c) {
 324         for (Object e : c)
 325             if (!contains(e))
 326                 return false;
 327         return true;
 328     }
 329 
 330     /**
 331      * {@inheritDoc}
 332      *
 333      * @implSpec
 334      * This implementation iterates over the specified collection, and adds
 335      * each object returned by the iterator to this collection, in turn.
 336      *
 337      * <p>Note that this implementation will throw an
 338      * {@code UnsupportedOperationException} unless {@code add} is


 341      * @throws UnsupportedOperationException {@inheritDoc}
 342      * @throws ClassCastException            {@inheritDoc}
 343      * @throws NullPointerException          {@inheritDoc}
 344      * @throws IllegalArgumentException      {@inheritDoc}
 345      * @throws IllegalStateException         {@inheritDoc}
 346      *
 347      * @see #add(Object)
 348      */
 349     public boolean addAll(Collection<? extends E> c) {
 350         boolean modified = false;
 351         for (E e : c)
 352             if (add(e))
 353                 modified = true;
 354         return modified;
 355     }
 356 
 357     /**
 358      * {@inheritDoc}
 359      *
 360      * @implSpec
 361      * This implementation iterates over this collection, checking each
 362      * element returned by the iterator in turn to see if it's contained
 363      * in the specified collection.  If it's so contained, it's removed from
 364      * this collection with the iterator's {@code remove} method.

 365      *
 366      * <p>Note that this implementation will throw an
 367      * {@code UnsupportedOperationException} if the iterator returned by the
 368      * {@code iterator} method does not implement the {@code remove} method
 369      * and this collection contains one or more elements in common with the
 370      * specified collection.
 371      *



 372      * @throws UnsupportedOperationException {@inheritDoc}
 373      * @throws ClassCastException            {@inheritDoc}
 374      * @throws NullPointerException          {@inheritDoc}
 375      *
 376      * @see #remove(Object)
 377      * @see #contains(Object)
 378      */
 379     public boolean removeAll(Collection<?> c) {
 380         Objects.requireNonNull(c);
 381         boolean modified = false;
 382         Iterator<?> it = iterator();
 383         while (it.hasNext()) {
 384             if (c.contains(it.next())) {
 385                 it.remove();
 386                 modified = true;
 387             }
 388         }
 389         return modified;
 390     }
 391 
 392     /**
 393      * {@inheritDoc}
 394      *
 395      * @implSpec
 396      * This implementation iterates over this collection, checking each
 397      * element returned by the iterator in turn to see if it's contained
 398      * in the specified collection.  If it's not so contained, it's removed
 399      * from this collection with the iterator's {@code remove} method.

 400      *
 401      * <p>Note that this implementation will throw an
 402      * {@code UnsupportedOperationException} if the iterator returned by the
 403      * {@code iterator} method does not implement the {@code remove} method
 404      * and this collection contains one or more elements not present in the
 405      * specified collection.
 406      *



 407      * @throws UnsupportedOperationException {@inheritDoc}
 408      * @throws ClassCastException            {@inheritDoc}
 409      * @throws NullPointerException          {@inheritDoc}
 410      *
 411      * @see #remove(Object)
 412      * @see #contains(Object)
 413      */
 414     public boolean retainAll(Collection<?> c) {
 415         Objects.requireNonNull(c);
 416         boolean modified = false;
 417         Iterator<E> it = iterator();
 418         while (it.hasNext()) {
 419             if (!c.contains(it.next())) {
 420                 it.remove();
 421                 modified = true;
 422             }
 423         }
 424         return modified;
 425     }
 426 


   1 /*
   2  * Copyright (c) 1997, 2019, 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
  22  * or visit www.oracle.com if you need additional information or have any


 299                     it.remove();
 300                     return true;
 301                 }
 302             }
 303         }
 304         return false;
 305     }
 306 
 307 
 308     // Bulk Operations
 309 
 310     /**
 311      * {@inheritDoc}
 312      *
 313      * @implSpec
 314      * This implementation iterates over the specified collection,
 315      * checking each element returned by the iterator in turn to see
 316      * if it's contained in this collection.  If all elements are so
 317      * contained {@code true} is returned, otherwise {@code false}.
 318      *
 319      * @implNote
 320      * {@inheritDoc}
 321      *
 322      * @throws ClassCastException            {@inheritDoc}
 323      * @throws NullPointerException          {@inheritDoc}
 324      * @see #contains(Object)
 325      */
 326     public boolean containsAll(Collection<?> c) {
 327         for (Object e : c)
 328             if (!contains(e))
 329                 return false;
 330         return true;
 331     }
 332 
 333     /**
 334      * {@inheritDoc}
 335      *
 336      * @implSpec
 337      * This implementation iterates over the specified collection, and adds
 338      * each object returned by the iterator to this collection, in turn.
 339      *
 340      * <p>Note that this implementation will throw an
 341      * {@code UnsupportedOperationException} unless {@code add} is


 344      * @throws UnsupportedOperationException {@inheritDoc}
 345      * @throws ClassCastException            {@inheritDoc}
 346      * @throws NullPointerException          {@inheritDoc}
 347      * @throws IllegalArgumentException      {@inheritDoc}
 348      * @throws IllegalStateException         {@inheritDoc}
 349      *
 350      * @see #add(Object)
 351      */
 352     public boolean addAll(Collection<? extends E> c) {
 353         boolean modified = false;
 354         for (E e : c)
 355             if (add(e))
 356                 modified = true;
 357         return modified;
 358     }
 359 
 360     /**
 361      * {@inheritDoc}
 362      *
 363      * @implSpec
 364      * This implementation iterates over each element of this collection by
 365      * obtaining an iterator from the {@code iterator} method. Each element
 366      * is passed to the {@code contains} method of the specified collection.
 367      * If {@code contains} returns {@code true}, the element is removed from this
 368      * collection with the iterator's {@code remove} method.
 369      *
 370      * <p>Note that this implementation will throw an
 371      * {@code UnsupportedOperationException} if the iterator returned by the
 372      * {@code iterator} method does not implement the {@code remove} method
 373      * and this collection contains one or more elements in common with the
 374      * specified collection.
 375      *
 376      * @implNote
 377      * {@inheritDoc}
 378      *
 379      * @throws UnsupportedOperationException {@inheritDoc}
 380      * @throws ClassCastException            {@inheritDoc}
 381      * @throws NullPointerException          {@inheritDoc}
 382      *
 383      * @see #remove(Object)
 384      * @see #contains(Object)
 385      */
 386     public boolean removeAll(Collection<?> c) {
 387         Objects.requireNonNull(c);
 388         boolean modified = false;
 389         Iterator<?> it = iterator();
 390         while (it.hasNext()) {
 391             if (c.contains(it.next())) {
 392                 it.remove();
 393                 modified = true;
 394             }
 395         }
 396         return modified;
 397     }
 398 
 399     /**
 400      * {@inheritDoc}
 401      *
 402      * @implSpec
 403      * This implementation iterates over each element of this collection by
 404      * obtaining an iterator from the {@code iterator} method. Each element
 405      * is passed to the {@code contains} method of the specified collection.
 406      * If {@code contains} returns {@code false}, the element is removed from
 407      * this collection with the iterator's {@code remove} method.
 408      *
 409      * <p>Note that this implementation will throw an
 410      * {@code UnsupportedOperationException} if the iterator returned by the
 411      * {@code iterator} method does not implement the {@code remove} method
 412      * and this collection contains one or more elements not present in the
 413      * specified collection.
 414      *
 415      * @implNote
 416      * {@inheritDoc}
 417      *
 418      * @throws UnsupportedOperationException {@inheritDoc}
 419      * @throws ClassCastException            {@inheritDoc}
 420      * @throws NullPointerException          {@inheritDoc}
 421      *
 422      * @see #remove(Object)
 423      * @see #contains(Object)
 424      */
 425     public boolean retainAll(Collection<?> c) {
 426         Objects.requireNonNull(c);
 427         boolean modified = false;
 428         Iterator<E> it = iterator();
 429         while (it.hasNext()) {
 430             if (!c.contains(it.next())) {
 431                 it.remove();
 432                 modified = true;
 433             }
 434         }
 435         return modified;
 436     }
 437 


< prev index next >