< prev index next >

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

Print this page




   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
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Predicate;
  29 import java.util.stream.Stream;
  30 import java.util.stream.StreamSupport;
  31 
  32 /**
  33  * The root interface in the <i>collection hierarchy</i>.  A collection
  34  * represents a group of objects, known as its <i>elements</i>.  Some
  35  * collections allow duplicate elements and others do not.  Some are ordered
  36  * and others unordered.  The JDK does not provide any <i>direct</i>
  37  * implementations of this interface: it provides implementations of more
  38  * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
  39  * is typically used to pass collections around and manipulate them where
  40  * maximum generality is desired.
  41  *
  42  * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
  43  * duplicate elements) should implement this interface directly.
  44  *
  45  * <p>All general-purpose <tt>Collection</tt> implementation classes (which
  46  * typically implement <tt>Collection</tt> indirectly through one of its
  47  * subinterfaces) should provide two "standard" constructors: a void (no
  48  * arguments) constructor, which creates an empty collection, and a
  49  * constructor with a single argument of type <tt>Collection</tt>, which
  50  * creates a new collection with the same elements as its argument.  In


 124  * @param <E> the type of elements in this collection
 125  *
 126  * @author  Josh Bloch
 127  * @author  Neal Gafter
 128  * @see     Set
 129  * @see     List
 130  * @see     Map
 131  * @see     SortedSet
 132  * @see     SortedMap
 133  * @see     HashSet
 134  * @see     TreeSet
 135  * @see     ArrayList
 136  * @see     LinkedList
 137  * @see     Vector
 138  * @see     Collections
 139  * @see     Arrays
 140  * @see     AbstractCollection
 141  * @since 1.2
 142  */
 143 
 144 public interface Collection<E> extends Iterable<E> {
 145     // Query Operations
 146 
 147     /**
 148      * Returns the number of elements in this collection.  If this collection
 149      * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 150      * <tt>Integer.MAX_VALUE</tt>.
 151      *
 152      * @return the number of elements in this collection
 153      */
 154     int size();
 155 
 156     /**
 157      * Returns <tt>true</tt> if this collection contains no elements.
 158      *
 159      * @return <tt>true</tt> if this collection contains no elements
 160      */
 161     boolean isEmpty();
 162 
 163     /**
 164      * Returns <tt>true</tt> if this collection contains the specified element.
 165      * More formally, returns <tt>true</tt> if and only if this collection
 166      * contains at least one element <tt>e</tt> such that
 167      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
 168      *
 169      * @param o element whose presence in this collection is to be tested
 170      * @return <tt>true</tt> if this collection contains the specified
 171      *         element
 172      * @throws ClassCastException if the type of the specified element
 173      *         is incompatible with this collection
 174      *         (<a href="#optional-restrictions">optional</a>)
 175      * @throws NullPointerException if the specified element is null and this
 176      *         collection does not permit null elements
 177      *         (<a href="#optional-restrictions">optional</a>)
 178      */
 179     boolean contains(Object o);
 180 








 181     /**
 182      * Returns an iterator over the elements in this collection.  There are no
 183      * guarantees concerning the order in which the elements are returned
 184      * (unless this collection is an instance of some class that provides a
 185      * guarantee).
 186      *
 187      * @return an <tt>Iterator</tt> over the elements in this collection
 188      */
 189     Iterator<E> iterator();
 190 
 191     /**
 192      * Returns an array containing all of the elements in this collection.
 193      * If this collection makes any guarantees as to what order its elements
 194      * are returned by its iterator, this method must return the elements in
 195      * the same order.
 196      *
 197      * <p>The returned array will be "safe" in that no references to it are
 198      * maintained by this collection.  (In other words, this method must
 199      * allocate a new array even if this collection is backed by an array).
 200      * The caller is thus free to modify the returned array.


 232      * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
 233      * The following code can be used to dump the collection into a newly
 234      * allocated array of <tt>String</tt>:
 235      *
 236      * <pre>
 237      *     String[] y = x.toArray(new String[0]);</pre>
 238      *
 239      * Note that <tt>toArray(new Object[0])</tt> is identical in function to
 240      * <tt>toArray()</tt>.
 241      *
 242      * @param <T> the runtime type of the array to contain the collection
 243      * @param a the array into which the elements of this collection are to be
 244      *        stored, if it is big enough; otherwise, a new array of the same
 245      *        runtime type is allocated for this purpose.
 246      * @return an array containing all of the elements in this collection
 247      * @throws ArrayStoreException if the runtime type of the specified array
 248      *         is not a supertype of the runtime type of every element in
 249      *         this collection
 250      * @throws NullPointerException if the specified array is null
 251      */
 252     <T> T[] toArray(T[] a);
 253 
 254     // Modification Operations
 255 
 256     /**
 257      * Ensures that this collection contains the specified element (optional
 258      * operation).  Returns <tt>true</tt> if this collection changed as a
 259      * result of the call.  (Returns <tt>false</tt> if this collection does
 260      * not permit duplicates and already contains the specified element.)<p>
 261      *
 262      * Collections that support this operation may place limitations on what
 263      * elements may be added to this collection.  In particular, some
 264      * collections will refuse to add <tt>null</tt> elements, and others will
 265      * impose restrictions on the type of elements that may be added.
 266      * Collection classes should clearly specify in their documentation any
 267      * restrictions on what elements may be added.<p>
 268      *
 269      * If a collection refuses to add a particular element for any reason
 270      * other than that it already contains the element, it <i>must</i> throw
 271      * an exception (rather than returning <tt>false</tt>).  This preserves
 272      * the invariant that a collection always contains the specified element


 293      * collection, if it is present (optional operation).  More formally,
 294      * removes an element <tt>e</tt> such that
 295      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
 296      * this collection contains one or more such elements.  Returns
 297      * <tt>true</tt> if this collection contained the specified element (or
 298      * equivalently, if this collection changed as a result of the call).
 299      *
 300      * @param o element to be removed from this collection, if present
 301      * @return <tt>true</tt> if an element was removed as a result of this call
 302      * @throws ClassCastException if the type of the specified element
 303      *         is incompatible with this collection
 304      *         (<a href="#optional-restrictions">optional</a>)
 305      * @throws NullPointerException if the specified element is null and this
 306      *         collection does not permit null elements
 307      *         (<a href="#optional-restrictions">optional</a>)
 308      * @throws UnsupportedOperationException if the <tt>remove</tt> operation
 309      *         is not supported by this collection
 310      */
 311     boolean remove(Object o);
 312 










 313 
 314     // Bulk Operations
 315 
 316     /**
 317      * Returns <tt>true</tt> if this collection contains all of the elements
 318      * in the specified collection.
 319      *
 320      * @param  c collection to be checked for containment in this collection
 321      * @return <tt>true</tt> if this collection contains all of the elements
 322      *         in the specified collection
 323      * @throws ClassCastException if the types of one or more elements
 324      *         in the specified collection are incompatible with this
 325      *         collection
 326      *         (<a href="#optional-restrictions">optional</a>)
 327      * @throws NullPointerException if the specified collection contains one
 328      *         or more null elements and this collection does not permit null
 329      *         elements
 330      *         (<a href="#optional-restrictions">optional</a>),
 331      *         or if the specified collection is null.
 332      * @see    #contains(Object)
 333      */
 334     boolean containsAll(Collection<?> c);
 335 










 336     /**
 337      * Adds all of the elements in the specified collection to this collection
 338      * (optional operation).  The behavior of this operation is undefined if
 339      * the specified collection is modified while the operation is in progress.
 340      * (This implies that the behavior of this call is undefined if the
 341      * specified collection is this collection, and this collection is
 342      * nonempty.)
 343      *
 344      * @param c collection containing elements to be added to this collection
 345      * @return <tt>true</tt> if this collection changed as a result of the call
 346      * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
 347      *         is not supported by this collection
 348      * @throws ClassCastException if the class of an element of the specified
 349      *         collection prevents it from being added to this collection
 350      * @throws NullPointerException if the specified collection contains a
 351      *         null element and this collection does not permit null elements,
 352      *         or if the specified collection is null
 353      * @throws IllegalArgumentException if some property of an element of the
 354      *         specified collection prevents it from being added to this
 355      *         collection


 367      *
 368      * @param c collection containing elements to be removed from this collection
 369      * @return <tt>true</tt> if this collection changed as a result of the
 370      *         call
 371      * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
 372      *         is not supported by this collection
 373      * @throws ClassCastException if the types of one or more elements
 374      *         in this collection are incompatible with the specified
 375      *         collection
 376      *         (<a href="#optional-restrictions">optional</a>)
 377      * @throws NullPointerException if this collection contains one or more
 378      *         null elements and the specified collection does not support
 379      *         null elements
 380      *         (<a href="#optional-restrictions">optional</a>),
 381      *         or if the specified collection is null
 382      * @see #remove(Object)
 383      * @see #contains(Object)
 384      */
 385     boolean removeAll(Collection<?> c);
 386 













 387     /**
 388      * Removes all of the elements of this collection that satisfy the given
 389      * predicate.  Errors or runtime exceptions thrown during iteration or by
 390      * the predicate are relayed to the caller.
 391      *
 392      * @implSpec
 393      * The default implementation traverses all elements of the collection using
 394      * its {@link #iterator}.  Each matching element is removed using
 395      * {@link Iterator#remove()}.  If the collection's iterator does not
 396      * support removal then an {@code UnsupportedOperationException} will be
 397      * thrown on the first matching element.
 398      *
 399      * @param filter a predicate which returns {@code true} for elements to be
 400      *        removed
 401      * @return {@code true} if any elements were removed
 402      * @throws NullPointerException if the specified filter is null
 403      * @throws UnsupportedOperationException if elements cannot be removed
 404      *         from this collection.  Implementations may throw this exception if a
 405      *         matching element cannot be removed or if, in general, removal is not
 406      *         supported.


 426      * specified collection.
 427      *
 428      * @param c collection containing elements to be retained in this collection
 429      * @return <tt>true</tt> if this collection changed as a result of the call
 430      * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
 431      *         is not supported by this collection
 432      * @throws ClassCastException if the types of one or more elements
 433      *         in this collection are incompatible with the specified
 434      *         collection
 435      *         (<a href="#optional-restrictions">optional</a>)
 436      * @throws NullPointerException if this collection contains one or more
 437      *         null elements and the specified collection does not permit null
 438      *         elements
 439      *         (<a href="#optional-restrictions">optional</a>),
 440      *         or if the specified collection is null
 441      * @see #remove(Object)
 442      * @see #contains(Object)
 443      */
 444     boolean retainAll(Collection<?> c);
 445 













 446     /**
 447      * Removes all of the elements from this collection (optional operation).
 448      * The collection will be empty after this method returns.
 449      *
 450      * @throws UnsupportedOperationException if the <tt>clear</tt> operation
 451      *         is not supported by this collection
 452      */
 453     void clear();
 454 
 455 
 456     // Comparison and hashing
 457 
 458     /**
 459      * Compares the specified object with this collection for equality. <p>
 460      *
 461      * While the <tt>Collection</tt> interface adds no stipulations to the
 462      * general contract for the <tt>Object.equals</tt>, programmers who
 463      * implement the <tt>Collection</tt> interface "directly" (in other words,
 464      * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
 465      * or a <tt>List</tt>) must exercise care if they choose to override the


 540      * from the collections's {@code Iterator}.  The spliterator inherits the
 541      * <em>fail-fast</em> properties of the collection's iterator.
 542      * <p>
 543      * The created {@code Spliterator} reports {@link Spliterator#SIZED}.
 544      *
 545      * @implNote
 546      * The created {@code Spliterator} additionally reports
 547      * {@link Spliterator#SUBSIZED}.
 548      *
 549      * <p>If a spliterator covers no elements then the reporting of additional
 550      * characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},
 551      * does not aid clients to control, specialize or simplify computation.
 552      * However, this does enable shared use of an immutable and empty
 553      * spliterator instance (see {@link Spliterators#emptySpliterator()}) for
 554      * empty collections, and enables clients to determine if such a spliterator
 555      * covers no elements.
 556      *
 557      * @return a {@code Spliterator} over the elements in this collection
 558      * @since 1.8
 559      */
 560     @Override
 561     default Spliterator<E> spliterator() {
 562         return Spliterators.spliterator(this, 0);
 563     }
 564 
 565     /**
 566      * Returns a sequential {@code Stream} with this collection as its source.
 567      *
 568      * <p>This method should be overridden when the {@link #spliterator()}
 569      * method cannot return a spliterator that is {@code IMMUTABLE},
 570      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
 571      * for details.)
 572      *
 573      * @implSpec
 574      * The default implementation creates a sequential {@code Stream} from the
 575      * collection's {@code Spliterator}.
 576      *
 577      * @return a sequential {@code Stream} over the elements in this collection
 578      * @since 1.8
 579      */
 580     default Stream<E> stream() {
 581         return StreamSupport.stream(spliterator(), false);
 582     }
 583 
 584     /**
 585      * Returns a possibly parallel {@code Stream} with this collection as its
 586      * source.  It is allowable for this method to return a sequential stream.
 587      *
 588      * <p>This method should be overridden when the {@link #spliterator()}
 589      * method cannot return a spliterator that is {@code IMMUTABLE},
 590      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
 591      * for details.)
 592      *
 593      * @implSpec
 594      * The default implementation creates a parallel {@code Stream} from the
 595      * collection's {@code Spliterator}.
 596      *
 597      * @return a possibly parallel {@code Stream} over the elements in this
 598      * collection
 599      * @since 1.8
 600      */
 601     default Stream<E> parallelStream() {
 602         return StreamSupport.stream(spliterator(), true);
 603     }
 604 }


   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
  23  * questions.
  24  */
  25 
  26 package javany.util;
  27 
  28 import javany.util.function.Predicate;
  29 
  30 import java.util.Objects;
  31 
  32 /**
  33  * The root interface in the <i>collection hierarchy</i>.  A collection
  34  * represents a group of objects, known as its <i>elements</i>.  Some
  35  * collections allow duplicate elements and others do not.  Some are ordered
  36  * and others unordered.  The JDK does not provide any <i>direct</i>
  37  * implementations of this interface: it provides implementations of more
  38  * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
  39  * is typically used to pass collections around and manipulate them where
  40  * maximum generality is desired.
  41  *
  42  * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
  43  * duplicate elements) should implement this interface directly.
  44  *
  45  * <p>All general-purpose <tt>Collection</tt> implementation classes (which
  46  * typically implement <tt>Collection</tt> indirectly through one of its
  47  * subinterfaces) should provide two "standard" constructors: a void (no
  48  * arguments) constructor, which creates an empty collection, and a
  49  * constructor with a single argument of type <tt>Collection</tt>, which
  50  * creates a new collection with the same elements as its argument.  In


 124  * @param <E> the type of elements in this collection
 125  *
 126  * @author  Josh Bloch
 127  * @author  Neal Gafter
 128  * @see     Set
 129  * @see     List
 130  * @see     Map
 131  * @see     SortedSet
 132  * @see     SortedMap
 133  * @see     HashSet
 134  * @see     TreeSet
 135  * @see     ArrayList
 136  * @see     LinkedList
 137  * @see     Vector
 138  * @see     Collections
 139  * @see     Arrays
 140  * @see     AbstractCollection
 141  * @since 1.2
 142  */
 143 
 144 public interface Collection<any E> extends javany.lang.Iterable<E> {
 145     // Query Operations
 146 
 147     /**
 148      * Returns the number of elements in this collection.  If this collection
 149      * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 150      * <tt>Integer.MAX_VALUE</tt>.
 151      *
 152      * @return the number of elements in this collection
 153      */
 154     int size();
 155 
 156     /**
 157      * Returns <tt>true</tt> if this collection contains no elements.
 158      *
 159      * @return <tt>true</tt> if this collection contains no elements
 160      */
 161     boolean isEmpty();
 162 
 163     /**
 164      * Returns <tt>true</tt> if this collection contains the specified element.
 165      * More formally, returns <tt>true</tt> if and only if this collection
 166      * contains at least one element <tt>e</tt> such that
 167      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
 168      *
 169      * @param o element whose presence in this collection is to be tested
 170      * @return <tt>true</tt> if this collection contains the specified
 171      *         element
 172      * @throws ClassCastException if the type of the specified element
 173      *         is incompatible with this collection
 174      *         (<a href="#optional-restrictions">optional</a>)
 175      * @throws NullPointerException if the specified element is null and this
 176      *         collection does not permit null elements
 177      *         (<a href="#optional-restrictions">optional</a>)
 178      */
 179     boolean contains(Object o);
 180 
 181     default boolean containsElement(E e) {
 182         Iterator<E> it = iterator();
 183         while (it.hasNext())
 184             if (Any.equals(e, it.next()))
 185                 return true;
 186         return false;
 187     }
 188 
 189     /**
 190      * Returns an iterator over the elements in this collection.  There are no
 191      * guarantees concerning the order in which the elements are returned
 192      * (unless this collection is an instance of some class that provides a
 193      * guarantee).
 194      *
 195      * @return an <tt>Iterator</tt> over the elements in this collection
 196      */
 197     Iterator<E> iterator();
 198 
 199     /**
 200      * Returns an array containing all of the elements in this collection.
 201      * If this collection makes any guarantees as to what order its elements
 202      * are returned by its iterator, this method must return the elements in
 203      * the same order.
 204      *
 205      * <p>The returned array will be "safe" in that no references to it are
 206      * maintained by this collection.  (In other words, this method must
 207      * allocate a new array even if this collection is backed by an array).
 208      * The caller is thus free to modify the returned array.


 240      * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
 241      * The following code can be used to dump the collection into a newly
 242      * allocated array of <tt>String</tt>:
 243      *
 244      * <pre>
 245      *     String[] y = x.toArray(new String[0]);</pre>
 246      *
 247      * Note that <tt>toArray(new Object[0])</tt> is identical in function to
 248      * <tt>toArray()</tt>.
 249      *
 250      * @param <T> the runtime type of the array to contain the collection
 251      * @param a the array into which the elements of this collection are to be
 252      *        stored, if it is big enough; otherwise, a new array of the same
 253      *        runtime type is allocated for this purpose.
 254      * @return an array containing all of the elements in this collection
 255      * @throws ArrayStoreException if the runtime type of the specified array
 256      *         is not a supertype of the runtime type of every element in
 257      *         this collection
 258      * @throws NullPointerException if the specified array is null
 259      */
 260     <any T> T[] toArray(T[] a);
 261 
 262     // Modification Operations
 263 
 264     /**
 265      * Ensures that this collection contains the specified element (optional
 266      * operation).  Returns <tt>true</tt> if this collection changed as a
 267      * result of the call.  (Returns <tt>false</tt> if this collection does
 268      * not permit duplicates and already contains the specified element.)<p>
 269      *
 270      * Collections that support this operation may place limitations on what
 271      * elements may be added to this collection.  In particular, some
 272      * collections will refuse to add <tt>null</tt> elements, and others will
 273      * impose restrictions on the type of elements that may be added.
 274      * Collection classes should clearly specify in their documentation any
 275      * restrictions on what elements may be added.<p>
 276      *
 277      * If a collection refuses to add a particular element for any reason
 278      * other than that it already contains the element, it <i>must</i> throw
 279      * an exception (rather than returning <tt>false</tt>).  This preserves
 280      * the invariant that a collection always contains the specified element


 301      * collection, if it is present (optional operation).  More formally,
 302      * removes an element <tt>e</tt> such that
 303      * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
 304      * this collection contains one or more such elements.  Returns
 305      * <tt>true</tt> if this collection contained the specified element (or
 306      * equivalently, if this collection changed as a result of the call).
 307      *
 308      * @param o element to be removed from this collection, if present
 309      * @return <tt>true</tt> if an element was removed as a result of this call
 310      * @throws ClassCastException if the type of the specified element
 311      *         is incompatible with this collection
 312      *         (<a href="#optional-restrictions">optional</a>)
 313      * @throws NullPointerException if the specified element is null and this
 314      *         collection does not permit null elements
 315      *         (<a href="#optional-restrictions">optional</a>)
 316      * @throws UnsupportedOperationException if the <tt>remove</tt> operation
 317      *         is not supported by this collection
 318      */
 319     boolean remove(Object o);
 320 
 321     default boolean removeElement(E e) {
 322         Iterator<E> it = iterator();
 323         while (it.hasNext()) {
 324             if (Any.equals(e, it.next())) {
 325                 it.remove();
 326                 return true;
 327             }
 328         }
 329         return false;
 330     }
 331 
 332     // Bulk Operations
 333 
 334     /**
 335      * Returns <tt>true</tt> if this collection contains all of the elements
 336      * in the specified collection.
 337      *
 338      * @param  c collection to be checked for containment in this collection
 339      * @return <tt>true</tt> if this collection contains all of the elements
 340      *         in the specified collection
 341      * @throws ClassCastException if the types of one or more elements
 342      *         in the specified collection are incompatible with this
 343      *         collection
 344      *         (<a href="#optional-restrictions">optional</a>)
 345      * @throws NullPointerException if the specified collection contains one
 346      *         or more null elements and this collection does not permit null
 347      *         elements
 348      *         (<a href="#optional-restrictions">optional</a>),
 349      *         or if the specified collection is null.
 350      * @see    #contains(Object)
 351      */
 352     boolean containsAll(Collection<?> c);
 353 
 354     default boolean containsAllElements(Collection<? extends E> c) {
 355         Iterator<? extends E> it = c.iterator();
 356         while (it.hasNext()) {
 357             if (!containsElement(it.next())) {
 358                 return false;
 359             }
 360         }
 361         return true;
 362     }
 363 
 364     /**
 365      * Adds all of the elements in the specified collection to this collection
 366      * (optional operation).  The behavior of this operation is undefined if
 367      * the specified collection is modified while the operation is in progress.
 368      * (This implies that the behavior of this call is undefined if the
 369      * specified collection is this collection, and this collection is
 370      * nonempty.)
 371      *
 372      * @param c collection containing elements to be added to this collection
 373      * @return <tt>true</tt> if this collection changed as a result of the call
 374      * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
 375      *         is not supported by this collection
 376      * @throws ClassCastException if the class of an element of the specified
 377      *         collection prevents it from being added to this collection
 378      * @throws NullPointerException if the specified collection contains a
 379      *         null element and this collection does not permit null elements,
 380      *         or if the specified collection is null
 381      * @throws IllegalArgumentException if some property of an element of the
 382      *         specified collection prevents it from being added to this
 383      *         collection


 395      *
 396      * @param c collection containing elements to be removed from this collection
 397      * @return <tt>true</tt> if this collection changed as a result of the
 398      *         call
 399      * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
 400      *         is not supported by this collection
 401      * @throws ClassCastException if the types of one or more elements
 402      *         in this collection are incompatible with the specified
 403      *         collection
 404      *         (<a href="#optional-restrictions">optional</a>)
 405      * @throws NullPointerException if this collection contains one or more
 406      *         null elements and the specified collection does not support
 407      *         null elements
 408      *         (<a href="#optional-restrictions">optional</a>),
 409      *         or if the specified collection is null
 410      * @see #remove(Object)
 411      * @see #contains(Object)
 412      */
 413     boolean removeAll(Collection<?> c);
 414 
 415     default boolean removeAllElements(Collection<E> c) {
 416         Objects.requireNonNull(c);
 417         boolean modified = false;
 418         Iterator<E> it = iterator();
 419         while (it.hasNext()) {
 420             if (c.containsElement(it.next())) {
 421                 it.remove();
 422                 modified = true;
 423             }
 424         }
 425         return modified;
 426     }
 427 
 428     /**
 429      * Removes all of the elements of this collection that satisfy the given
 430      * predicate.  Errors or runtime exceptions thrown during iteration or by
 431      * the predicate are relayed to the caller.
 432      *
 433      * @implSpec
 434      * The default implementation traverses all elements of the collection using
 435      * its {@link #iterator}.  Each matching element is removed using
 436      * {@link Iterator#remove()}.  If the collection's iterator does not
 437      * support removal then an {@code UnsupportedOperationException} will be
 438      * thrown on the first matching element.
 439      *
 440      * @param filter a predicate which returns {@code true} for elements to be
 441      *        removed
 442      * @return {@code true} if any elements were removed
 443      * @throws NullPointerException if the specified filter is null
 444      * @throws UnsupportedOperationException if elements cannot be removed
 445      *         from this collection.  Implementations may throw this exception if a
 446      *         matching element cannot be removed or if, in general, removal is not
 447      *         supported.


 467      * specified collection.
 468      *
 469      * @param c collection containing elements to be retained in this collection
 470      * @return <tt>true</tt> if this collection changed as a result of the call
 471      * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
 472      *         is not supported by this collection
 473      * @throws ClassCastException if the types of one or more elements
 474      *         in this collection are incompatible with the specified
 475      *         collection
 476      *         (<a href="#optional-restrictions">optional</a>)
 477      * @throws NullPointerException if this collection contains one or more
 478      *         null elements and the specified collection does not permit null
 479      *         elements
 480      *         (<a href="#optional-restrictions">optional</a>),
 481      *         or if the specified collection is null
 482      * @see #remove(Object)
 483      * @see #contains(Object)
 484      */
 485     boolean retainAll(Collection<?> c);
 486 
 487     default boolean retainAllElements(Collection<E> c) {
 488         Objects.requireNonNull(c);
 489         boolean modified = false;
 490         Iterator<E> it = iterator();
 491         while (it.hasNext()) {
 492             if (!c.containsElement(it.next())) {
 493                 it.remove();
 494                 modified = true;
 495             }
 496         }
 497         return modified;
 498     }
 499 
 500     /**
 501      * Removes all of the elements from this collection (optional operation).
 502      * The collection will be empty after this method returns.
 503      *
 504      * @throws UnsupportedOperationException if the <tt>clear</tt> operation
 505      *         is not supported by this collection
 506      */
 507     void clear();
 508 
 509 
 510     // Comparison and hashing
 511 
 512     /**
 513      * Compares the specified object with this collection for equality. <p>
 514      *
 515      * While the <tt>Collection</tt> interface adds no stipulations to the
 516      * general contract for the <tt>Object.equals</tt>, programmers who
 517      * implement the <tt>Collection</tt> interface "directly" (in other words,
 518      * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
 519      * or a <tt>List</tt>) must exercise care if they choose to override the


 594      * from the collections's {@code Iterator}.  The spliterator inherits the
 595      * <em>fail-fast</em> properties of the collection's iterator.
 596      * <p>
 597      * The created {@code Spliterator} reports {@link Spliterator#SIZED}.
 598      *
 599      * @implNote
 600      * The created {@code Spliterator} additionally reports
 601      * {@link Spliterator#SUBSIZED}.
 602      *
 603      * <p>If a spliterator covers no elements then the reporting of additional
 604      * characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},
 605      * does not aid clients to control, specialize or simplify computation.
 606      * However, this does enable shared use of an immutable and empty
 607      * spliterator instance (see {@link Spliterators#emptySpliterator()}) for
 608      * empty collections, and enables clients to determine if such a spliterator
 609      * covers no elements.
 610      *
 611      * @return a {@code Spliterator} over the elements in this collection
 612      * @since 1.8
 613      */
 614 //    @Override
 615 //    default Spliterator<E> spliterator() {
 616 //        return Spliterators.spliterator(this, 0);
 617 //    }
 618 
 619     /**
 620      * Returns a sequential {@code Stream} with this collection as its source.
 621      *
 622      * <p>This method should be overridden when the {@link #spliterator()}
 623      * method cannot return a spliterator that is {@code IMMUTABLE},
 624      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
 625      * for details.)
 626      *
 627      * @implSpec
 628      * The default implementation creates a sequential {@code Stream} from the
 629      * collection's {@code Spliterator}.
 630      *
 631      * @return a sequential {@code Stream} over the elements in this collection
 632      * @since 1.8
 633      */
 634 //    default Stream<E> stream() {
 635 //        return StreamSupport.stream(spliterator(), false);
 636 //    }
 637 
 638     /**
 639      * Returns a possibly parallel {@code Stream} with this collection as its
 640      * source.  It is allowable for this method to return a sequential stream.
 641      *
 642      * <p>This method should be overridden when the {@link #spliterator()}
 643      * method cannot return a spliterator that is {@code IMMUTABLE},
 644      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
 645      * for details.)
 646      *
 647      * @implSpec
 648      * The default implementation creates a parallel {@code Stream} from the
 649      * collection's {@code Spliterator}.
 650      *
 651      * @return a possibly parallel {@code Stream} over the elements in this
 652      * collection
 653      * @since 1.8
 654      */
 655 //    default Stream<E> parallelStream() {
 656 //        return StreamSupport.stream(spliterator(), true);
 657 //    }
 658 }
< prev index next >