1 /*
   2  * Copyright (c) 1997, 2017, 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
  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 {@code Set} and {@code List}.  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 {@code Collection} implementation classes (which
  46  * typically implement {@code Collection} 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 {@code Collection}, which
  50  * creates a new collection with the same elements as its argument.  In
  51  * effect, the latter constructor allows the user to copy any collection,
  52  * producing an equivalent collection of the desired implementation type.
  53  * There is no way to enforce this convention (as interfaces cannot contain
  54  * constructors) but all of the general-purpose {@code Collection}
  55  * implementations in the Java platform libraries comply.
  56  *
  57  * <p>Certain methods are specified to be
  58  * <i>optional</i>. If a collection implementation doesn't implement a
  59  * particular operation, it should define the corresponding method to throw
  60  * {@code UnsupportedOperationException}. Such methods are marked "optional
  61  * operation" in method specifications of the collections interfaces.
  62  *
  63  * <p><a id="optional-restrictions"></a>Some collection implementations
  64  * have restrictions on the elements that they may contain.
  65  * For example, some implementations prohibit null elements,
  66  * and some have restrictions on the types of their elements.  Attempting to
  67  * add an ineligible element throws an unchecked exception, typically
  68  * {@code NullPointerException} or {@code ClassCastException}.  Attempting
  69  * to query the presence of an ineligible element may throw an exception,
  70  * or it may simply return false; some implementations will exhibit the former
  71  * behavior and some will exhibit the latter.  More generally, attempting an
  72  * operation on an ineligible element whose completion would not result in
  73  * the insertion of an ineligible element into the collection may throw an
  74  * exception or it may succeed, at the option of the implementation.
  75  * Such exceptions are marked as "optional" in the specification for this
  76  * interface.
  77  *
  78  * <p>It is up to each collection to determine its own synchronization
  79  * policy.  In the absence of a stronger guarantee by the
  80  * implementation, undefined behavior may result from the invocation
  81  * of any method on a collection that is being mutated by another
  82  * thread; this includes direct invocations, passing the collection to
  83  * a method that might perform invocations, and using an existing
  84  * iterator to examine the collection.
  85  *
  86  * <p>Many methods in Collections Framework interfaces are defined in
  87  * terms of the {@link Object#equals(Object) equals} method.  For example,
  88  * the specification for the {@link #contains(Object) contains(Object o)}
  89  * method says: "returns {@code true} if and only if this collection
  90  * contains at least one element {@code e} such that
  91  * {@code (o==null ? e==null : o.equals(e))}."  This specification should
  92  * <i>not</i> be construed to imply that invoking {@code Collection.contains}
  93  * with a non-null argument {@code o} will cause {@code o.equals(e)} to be
  94  * invoked for any element {@code e}.  Implementations are free to implement
  95  * optimizations whereby the {@code equals} invocation is avoided, for
  96  * example, by first comparing the hash codes of the two elements.  (The
  97  * {@link Object#hashCode()} specification guarantees that two objects with
  98  * unequal hash codes cannot be equal.)  More generally, implementations of
  99  * the various Collections Framework interfaces are free to take advantage of
 100  * the specified behavior of underlying {@link Object} methods wherever the
 101  * implementor deems it appropriate.
 102  *
 103  * <p>Some collection operations which perform recursive traversal of the
 104  * collection may fail with an exception for self-referential instances where
 105  * the collection directly or indirectly contains itself. This includes the
 106  * {@code clone()}, {@code equals()}, {@code hashCode()} and {@code toString()}
 107  * methods. Implementations may optionally handle the self-referential scenario,
 108  * however most current implementations do not do so.
 109  *
 110  * <h2><a id="view">View Collections</a></h2>
 111  *
 112  * <p>Most collections manage storage for elements they contain. By contrast, <i>view
 113  * collections</i> themselves do not store elements, but instead they rely on a
 114  * backing collection to store the actual elements. Operations that are not handled
 115  * by the view collection itself are delegated to the backing collection. Examples of
 116  * view collections include the wrapper collections returned by methods such as
 117  * {@link Collections#checkedCollection Collections.checkedCollection},
 118  * {@link Collections#synchronizedCollection Collections.synchronizedCollection}, and
 119  * {@link Collections#unmodifiableCollection Collections.unmodifiableCollection}.
 120  * Other examples of view collections include collections that provide a
 121  * different representation of the same elements, for example, as
 122  * provided by {@link List#subList List.subList},
 123  * {@link NavigableSet#subSet NavigableSet.subSet}, or
 124  * {@link Map#entrySet Map.entrySet}.
 125  * Any changes made to the backing collection are visible in the view collection.
 126  * Correspondingly, any changes made to the view collection &mdash; if changes
 127  * are permitted &mdash; are written through to the backing collection.
 128  * Although they technically aren't collections, instances of
 129  * {@link Iterator} and {@link ListIterator} can also allow modifications
 130  * to be written through to the backing collection, and in some cases,
 131  * modifications to the backing collection will be visible to the Iterator
 132  * during iteration.
 133  *
 134  * <h2><a id="unmodifiable">Unmodifiable Collections</a></h2>
 135  *
 136  * <p>Certain methods of this interface are considered "destructive" and are called
 137  * "mutator" methods in that they modify the group of objects contained within
 138  * the collection on which they operate. They can be specified to throw
 139  * {@code UnsupportedOperationException} if this collection implementation
 140  * does not support the operation. Such methods should (but are not required
 141  * to) throw an {@code UnsupportedOperationException} if the invocation would
 142  * have no effect on the collection. For example, consider a collection that
 143  * does not support the {@link #add add} operation. What will happen if the
 144  * {@link #addAll addAll} method is invoked on this collection, with an empty
 145  * collection as the argument? The addition of zero elements has no effect,
 146  * so it is permissible for this collection simply to do nothing and not to throw
 147  * an exception. However, it is recommended that such cases throw an exception
 148  * unconditionally, as throwing only in certain cases can lead to
 149  * programming errors.
 150  *
 151  * <p>An <i>unmodifiable collection</i> is a collection, all of whose
 152  * mutator methods (as defined above) are specified to throw
 153  * {@code UnsupportedOperationException}. Such a collection thus cannot be
 154  * modified by calling any methods on it. For a collection to be properly
 155  * unmodifiable, any view collections derived from it must also be unmodifiable.
 156  * For example, if a List is unmodifiable, the List returned by
 157  * {@link List#subList List.subList} is also unmodifiable.
 158  *
 159  * <p>An unmodifiable collection is not necessarily immutable. If the
 160  * contained elements are mutable, the entire collection is clearly
 161  * mutable, even though it might be unmodifiable. For example, consider
 162  * two unmodifiable lists containing mutable elements. The result of calling
 163  * {@code list1.equals(list2)} might differ from one call to the next if
 164  * the elements had been mutated, even though both lists are unmodifiable.
 165  * However, if an unmodifiable collection contains all immutable elements,
 166  * it can be considered effectively immutable.
 167  *
 168  * <h2><a id="unmodview">Unmodifiable View Collections</a></h2>
 169  *
 170  * <p>An <i>unmodifiable view collection</i> is a collection that is unmodifiable
 171  * and that is also a view onto a backing collection. Its mutator methods throw
 172  * {@code UnsupportedOperationException}, as described above, while
 173  * reading and querying methods are delegated to the backing collection.
 174  * The effect is to provide read-only access to the backing collection.
 175  * This is useful for a component to provide users with read access to
 176  * an internal collection, while preventing them from modifying such
 177  * collections unexpectedly. Examples of unmodifiable view collections
 178  * are those returned by the
 179  * {@link Collections#unmodifiableCollection Collections.unmodifiableCollection},
 180  * {@link Collections#unmodifiableList Collections.unmodifiableList}, and
 181  * related methods.
 182  *
 183  * <p>Note that changes to the backing collection might still be possible,
 184  * and if they occur, they are visible through the unmodifiable view. Thus,
 185  * an unmodifiable view collection is not necessarily immutable. However,
 186  * if the backing collection of an unmodifiable view is effectively immutable,
 187  * or if the only reference to the backing collection is through an
 188  * unmodifiable view, the view can be considered effectively immutable.
 189  *
 190  * <p>This interface is a member of the
 191  * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
 192  * Java Collections Framework</a>.
 193  *
 194  * @implSpec
 195  * The default method implementations (inherited or otherwise) do not apply any
 196  * synchronization protocol.  If a {@code Collection} implementation has a
 197  * specific synchronization protocol, then it must override default
 198  * implementations to apply that protocol.
 199  *
 200  * @param <E> the type of elements in this collection
 201  *
 202  * @author  Josh Bloch
 203  * @author  Neal Gafter
 204  * @see     Set
 205  * @see     List
 206  * @see     Map
 207  * @see     SortedSet
 208  * @see     SortedMap
 209  * @see     HashSet
 210  * @see     TreeSet
 211  * @see     ArrayList
 212  * @see     LinkedList
 213  * @see     Vector
 214  * @see     Collections
 215  * @see     Arrays
 216  * @see     AbstractCollection
 217  * @since 1.2
 218  */
 219 
 220 public interface Collection<E> extends Iterable<E> {
 221     // Query Operations
 222 
 223     /**
 224      * Returns the number of elements in this collection.  If this collection
 225      * contains more than {@code Integer.MAX_VALUE} elements, returns
 226      * {@code Integer.MAX_VALUE}.
 227      *
 228      * @return the number of elements in this collection
 229      */
 230     int size();
 231 
 232     /**
 233      * Returns {@code true} if this collection contains no elements.
 234      *
 235      * @return {@code true} if this collection contains no elements
 236      */
 237     boolean isEmpty();
 238 
 239     /**
 240      * Returns {@code true} if this collection contains the specified element.
 241      * More formally, returns {@code true} if and only if this collection
 242      * contains at least one element {@code e} such that
 243      * {@code Objects.equals(o, e)}.
 244      *
 245      * @param o element whose presence in this collection is to be tested
 246      * @return {@code true} if this collection contains the specified
 247      *         element
 248      * @throws ClassCastException if the type of the specified element
 249      *         is incompatible with this collection
 250      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 251      * @throws NullPointerException if the specified element is null and this
 252      *         collection does not permit null elements
 253      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 254      */
 255     boolean contains(Object o);
 256 
 257     /**
 258      * Returns an iterator over the elements in this collection.  There are no
 259      * guarantees concerning the order in which the elements are returned
 260      * (unless this collection is an instance of some class that provides a
 261      * guarantee).
 262      *
 263      * @return an {@code Iterator} over the elements in this collection
 264      */
 265     Iterator<E> iterator();
 266 
 267     /**
 268      * Returns an array containing all of the elements in this collection.
 269      * If this collection makes any guarantees as to what order its elements
 270      * are returned by its iterator, this method must return the elements in
 271      * the same order. The returned array's {@linkplain Class#getComponentType
 272      * runtime component type} is {@code Object}.
 273      *
 274      * <p>The returned array will be "safe" in that no references to it are
 275      * maintained by this collection.  (In other words, this method must
 276      * allocate a new array even if this collection is backed by an array).
 277      * The caller is thus free to modify the returned array.
 278      *
 279      * <p>This method acts as bridge between array-based and collection-based
 280      * APIs.
 281      *
 282      * @return an array, whose {@linkplain Class#getComponentType runtime component
 283      * type} is {@code Object}, containing all of the elements in this collection
 284      */
 285     Object[] toArray();
 286 
 287     /**
 288      * Returns an array containing all of the elements in this collection;
 289      * the runtime type of the returned array is that of the specified array.
 290      * If the collection fits in the specified array, it is returned therein.
 291      * Otherwise, a new array is allocated with the runtime type of the
 292      * specified array and the size of this collection.
 293      *
 294      * <p>If this collection fits in the specified array with room to spare
 295      * (i.e., the array has more elements than this collection), the element
 296      * in the array immediately following the end of the collection is set to
 297      * {@code null}.  (This is useful in determining the length of this
 298      * collection <i>only</i> if the caller knows that this collection does
 299      * not contain any {@code null} elements.)
 300      *
 301      * <p>If this collection makes any guarantees as to what order its elements
 302      * are returned by its iterator, this method must return the elements in
 303      * the same order.
 304      *
 305      * <p>Like the {@link #toArray()} method, this method acts as bridge between
 306      * array-based and collection-based APIs.  Further, this method allows
 307      * precise control over the runtime type of the output array, and may,
 308      * under certain circumstances, be used to save allocation costs.
 309      *
 310      * <p>Suppose {@code x} is a collection known to contain only strings.
 311      * The following code can be used to dump the collection into a newly
 312      * allocated array of {@code String}:
 313      *
 314      * <pre>
 315      *     String[] y = x.toArray(new String[0]);</pre>
 316      *
 317      * Note that {@code toArray(new Object[0])} is identical in function to
 318      * {@code toArray()}.
 319      *
 320      * @param <T> the component type of the array to contain the collection
 321      * @param a the array into which the elements of this collection are to be
 322      *        stored, if it is big enough; otherwise, a new array of the same
 323      *        runtime type is allocated for this purpose.
 324      * @return an array containing all of the elements in this collection
 325      * @throws ArrayStoreException if the runtime type of any element in this
 326      *         collection is not assignable to the {@linkplain Class#getComponentType
 327      *         runtime component type} of the specified array
 328      * @throws NullPointerException if the specified array is null
 329      */
 330     <T> T[] toArray(T[] a);
 331 
 332     // Modification Operations
 333 
 334     /**
 335      * Ensures that this collection contains the specified element (optional
 336      * operation).  Returns {@code true} if this collection changed as a
 337      * result of the call.  (Returns {@code false} if this collection does
 338      * not permit duplicates and already contains the specified element.)<p>
 339      *
 340      * Collections that support this operation may place limitations on what
 341      * elements may be added to this collection.  In particular, some
 342      * collections will refuse to add {@code null} elements, and others will
 343      * impose restrictions on the type of elements that may be added.
 344      * Collection classes should clearly specify in their documentation any
 345      * restrictions on what elements may be added.<p>
 346      *
 347      * If a collection refuses to add a particular element for any reason
 348      * other than that it already contains the element, it <i>must</i> throw
 349      * an exception (rather than returning {@code false}).  This preserves
 350      * the invariant that a collection always contains the specified element
 351      * after this call returns.
 352      *
 353      * @param e element whose presence in this collection is to be ensured
 354      * @return {@code true} if this collection changed as a result of the
 355      *         call
 356      * @throws UnsupportedOperationException if the {@code add} operation
 357      *         is not supported by this collection
 358      * @throws ClassCastException if the class of the specified element
 359      *         prevents it from being added to this collection
 360      * @throws NullPointerException if the specified element is null and this
 361      *         collection does not permit null elements
 362      * @throws IllegalArgumentException if some property of the element
 363      *         prevents it from being added to this collection
 364      * @throws IllegalStateException if the element cannot be added at this
 365      *         time due to insertion restrictions
 366      */
 367     boolean add(E e);
 368 
 369     /**
 370      * Removes a single instance of the specified element from this
 371      * collection, if it is present (optional operation).  More formally,
 372      * removes an element {@code e} such that
 373      * {@code Objects.equals(o, e)}, if
 374      * this collection contains one or more such elements.  Returns
 375      * {@code true} if this collection contained the specified element (or
 376      * equivalently, if this collection changed as a result of the call).
 377      *
 378      * @param o element to be removed from this collection, if present
 379      * @return {@code true} if an element was removed as a result of this call
 380      * @throws ClassCastException if the type of the specified element
 381      *         is incompatible with this collection
 382      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 383      * @throws NullPointerException if the specified element is null and this
 384      *         collection does not permit null elements
 385      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 386      * @throws UnsupportedOperationException if the {@code remove} operation
 387      *         is not supported by this collection
 388      */
 389     boolean remove(Object o);
 390 
 391 
 392     // Bulk Operations
 393 
 394     /**
 395      * Returns {@code true} if this collection contains all of the elements
 396      * in the specified collection.
 397      *
 398      * @param  c collection to be checked for containment in this collection
 399      * @return {@code true} if this collection contains all of the elements
 400      *         in the specified collection
 401      * @throws ClassCastException if the types of one or more elements
 402      *         in the specified collection are incompatible with this
 403      *         collection
 404      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 405      * @throws NullPointerException if the specified collection contains one
 406      *         or more null elements and this collection does not permit null
 407      *         elements
 408      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
 409      *         or if the specified collection is null.
 410      * @see    #contains(Object)
 411      */
 412     boolean containsAll(Collection<?> c);
 413 
 414     /**
 415      * Adds all of the elements in the specified collection to this collection
 416      * (optional operation).  The behavior of this operation is undefined if
 417      * the specified collection is modified while the operation is in progress.
 418      * (This implies that the behavior of this call is undefined if the
 419      * specified collection is this collection, and this collection is
 420      * nonempty.)
 421      *
 422      * @param c collection containing elements to be added to this collection
 423      * @return {@code true} if this collection changed as a result of the call
 424      * @throws UnsupportedOperationException if the {@code addAll} operation
 425      *         is not supported by this collection
 426      * @throws ClassCastException if the class of an element of the specified
 427      *         collection prevents it from being added to this collection
 428      * @throws NullPointerException if the specified collection contains a
 429      *         null element and this collection does not permit null elements,
 430      *         or if the specified collection is null
 431      * @throws IllegalArgumentException if some property of an element of the
 432      *         specified collection prevents it from being added to this
 433      *         collection
 434      * @throws IllegalStateException if not all the elements can be added at
 435      *         this time due to insertion restrictions
 436      * @see #add(Object)
 437      */
 438     boolean addAll(Collection<? extends E> c);
 439 
 440     /**
 441      * Removes all of this collection's elements that are also contained in the
 442      * specified collection (optional operation).  After this call returns,
 443      * this collection will contain no elements in common with the specified
 444      * collection.
 445      *
 446      * @param c collection containing elements to be removed from this collection
 447      * @return {@code true} if this collection changed as a result of the
 448      *         call
 449      * @throws UnsupportedOperationException if the {@code removeAll} method
 450      *         is not supported by this collection
 451      * @throws ClassCastException if the types of one or more elements
 452      *         in this collection are incompatible with the specified
 453      *         collection
 454      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 455      * @throws NullPointerException if this collection contains one or more
 456      *         null elements and the specified collection does not support
 457      *         null elements
 458      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
 459      *         or if the specified collection is null
 460      * @see #remove(Object)
 461      * @see #contains(Object)
 462      */
 463     boolean removeAll(Collection<?> c);
 464 
 465     /**
 466      * Removes all of the elements of this collection that satisfy the given
 467      * predicate.  Errors or runtime exceptions thrown during iteration or by
 468      * the predicate are relayed to the caller.
 469      *
 470      * @implSpec
 471      * The default implementation traverses all elements of the collection using
 472      * its {@link #iterator}.  Each matching element is removed using
 473      * {@link Iterator#remove()}.  If the collection's iterator does not
 474      * support removal then an {@code UnsupportedOperationException} will be
 475      * thrown on the first matching element.
 476      *
 477      * @param filter a predicate which returns {@code true} for elements to be
 478      *        removed
 479      * @return {@code true} if any elements were removed
 480      * @throws NullPointerException if the specified filter is null
 481      * @throws UnsupportedOperationException if elements cannot be removed
 482      *         from this collection.  Implementations may throw this exception if a
 483      *         matching element cannot be removed or if, in general, removal is not
 484      *         supported.
 485      * @since 1.8
 486      */
 487     default boolean removeIf(Predicate<? super E> filter) {
 488         Objects.requireNonNull(filter);
 489         boolean removed = false;
 490         final Iterator<E> each = iterator();
 491         while (each.hasNext()) {
 492             if (filter.test(each.next())) {
 493                 each.remove();
 494                 removed = true;
 495             }
 496         }
 497         return removed;
 498     }
 499 
 500     /**
 501      * Retains only the elements in this collection that are contained in the
 502      * specified collection (optional operation).  In other words, removes from
 503      * this collection all of its elements that are not contained in the
 504      * specified collection.
 505      *
 506      * @param c collection containing elements to be retained in this collection
 507      * @return {@code true} if this collection changed as a result of the call
 508      * @throws UnsupportedOperationException if the {@code retainAll} operation
 509      *         is not supported by this collection
 510      * @throws ClassCastException if the types of one or more elements
 511      *         in this collection are incompatible with the specified
 512      *         collection
 513      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
 514      * @throws NullPointerException if this collection contains one or more
 515      *         null elements and the specified collection does not permit null
 516      *         elements
 517      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
 518      *         or if the specified collection is null
 519      * @see #remove(Object)
 520      * @see #contains(Object)
 521      */
 522     boolean retainAll(Collection<?> c);
 523 
 524     /**
 525      * Removes all of the elements from this collection (optional operation).
 526      * The collection will be empty after this method returns.
 527      *
 528      * @throws UnsupportedOperationException if the {@code clear} operation
 529      *         is not supported by this collection
 530      */
 531     void clear();
 532 
 533 
 534     // Comparison and hashing
 535 
 536     /**
 537      * Compares the specified object with this collection for equality. <p>
 538      *
 539      * While the {@code Collection} interface adds no stipulations to the
 540      * general contract for the {@code Object.equals}, programmers who
 541      * implement the {@code Collection} interface "directly" (in other words,
 542      * create a class that is a {@code Collection} but is not a {@code Set}
 543      * or a {@code List}) must exercise care if they choose to override the
 544      * {@code Object.equals}.  It is not necessary to do so, and the simplest
 545      * course of action is to rely on {@code Object}'s implementation, but
 546      * the implementor may wish to implement a "value comparison" in place of
 547      * the default "reference comparison."  (The {@code List} and
 548      * {@code Set} interfaces mandate such value comparisons.)<p>
 549      *
 550      * The general contract for the {@code Object.equals} method states that
 551      * equals must be symmetric (in other words, {@code a.equals(b)} if and
 552      * only if {@code b.equals(a)}).  The contracts for {@code List.equals}
 553      * and {@code Set.equals} state that lists are only equal to other lists,
 554      * and sets to other sets.  Thus, a custom {@code equals} method for a
 555      * collection class that implements neither the {@code List} nor
 556      * {@code Set} interface must return {@code false} when this collection
 557      * is compared to any list or set.  (By the same logic, it is not possible
 558      * to write a class that correctly implements both the {@code Set} and
 559      * {@code List} interfaces.)
 560      *
 561      * @param o object to be compared for equality with this collection
 562      * @return {@code true} if the specified object is equal to this
 563      * collection
 564      *
 565      * @see Object#equals(Object)
 566      * @see Set#equals(Object)
 567      * @see List#equals(Object)
 568      */
 569     boolean equals(Object o);
 570 
 571     /**
 572      * Returns the hash code value for this collection.  While the
 573      * {@code Collection} interface adds no stipulations to the general
 574      * contract for the {@code Object.hashCode} method, programmers should
 575      * take note that any class that overrides the {@code Object.equals}
 576      * method must also override the {@code Object.hashCode} method in order
 577      * to satisfy the general contract for the {@code Object.hashCode} method.
 578      * In particular, {@code c1.equals(c2)} implies that
 579      * {@code c1.hashCode()==c2.hashCode()}.
 580      *
 581      * @return the hash code value for this collection
 582      *
 583      * @see Object#hashCode()
 584      * @see Object#equals(Object)
 585      */
 586     int hashCode();
 587 
 588     /**
 589      * Creates a {@link Spliterator} over the elements in this collection.
 590      *
 591      * Implementations should document characteristic values reported by the
 592      * spliterator.  Such characteristic values are not required to be reported
 593      * if the spliterator reports {@link Spliterator#SIZED} and this collection
 594      * contains no elements.
 595      *
 596      * <p>The default implementation should be overridden by subclasses that
 597      * can return a more efficient spliterator.  In order to
 598      * preserve expected laziness behavior for the {@link #stream()} and
 599      * {@link #parallelStream()} methods, spliterators should either have the
 600      * characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be
 601      * <em><a href="Spliterator.html#binding">late-binding</a></em>.
 602      * If none of these is practical, the overriding class should describe the
 603      * spliterator's documented policy of binding and structural interference,
 604      * and should override the {@link #stream()} and {@link #parallelStream()}
 605      * methods to create streams using a {@code Supplier} of the spliterator,
 606      * as in:
 607      * <pre>{@code
 608      *     Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)
 609      * }</pre>
 610      * <p>These requirements ensure that streams produced by the
 611      * {@link #stream()} and {@link #parallelStream()} methods will reflect the
 612      * contents of the collection as of initiation of the terminal stream
 613      * operation.
 614      *
 615      * @implSpec
 616      * The default implementation creates a
 617      * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator
 618      * from the collection's {@code Iterator}.  The spliterator inherits the
 619      * <em>fail-fast</em> properties of the collection's iterator.
 620      * <p>
 621      * The created {@code Spliterator} reports {@link Spliterator#SIZED}.
 622      *
 623      * @implNote
 624      * The created {@code Spliterator} additionally reports
 625      * {@link Spliterator#SUBSIZED}.
 626      *
 627      * <p>If a spliterator covers no elements then the reporting of additional
 628      * characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},
 629      * does not aid clients to control, specialize or simplify computation.
 630      * However, this does enable shared use of an immutable and empty
 631      * spliterator instance (see {@link Spliterators#emptySpliterator()}) for
 632      * empty collections, and enables clients to determine if such a spliterator
 633      * covers no elements.
 634      *
 635      * @return a {@code Spliterator} over the elements in this collection
 636      * @since 1.8
 637      */
 638     @Override
 639     default Spliterator<E> spliterator() {
 640         return Spliterators.spliterator(this, 0);
 641     }
 642 
 643     /**
 644      * Returns a sequential {@code Stream} with this collection as its source.
 645      *
 646      * <p>This method should be overridden when the {@link #spliterator()}
 647      * method cannot return a spliterator that is {@code IMMUTABLE},
 648      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
 649      * for details.)
 650      *
 651      * @implSpec
 652      * The default implementation creates a sequential {@code Stream} from the
 653      * collection's {@code Spliterator}.
 654      *
 655      * @return a sequential {@code Stream} over the elements in this collection
 656      * @since 1.8
 657      */
 658     default Stream<E> stream() {
 659         return StreamSupport.stream(spliterator(), false);
 660     }
 661 
 662     /**
 663      * Returns a possibly parallel {@code Stream} with this collection as its
 664      * source.  It is allowable for this method to return a sequential stream.
 665      *
 666      * <p>This method should be overridden when the {@link #spliterator()}
 667      * method cannot return a spliterator that is {@code IMMUTABLE},
 668      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
 669      * for details.)
 670      *
 671      * @implSpec
 672      * The default implementation creates a parallel {@code Stream} from the
 673      * collection's {@code Spliterator}.
 674      *
 675      * @return a possibly parallel {@code Stream} over the elements in this
 676      * collection
 677      * @since 1.8
 678      */
 679     default Stream<E> parallelStream() {
 680         return StreamSupport.stream(spliterator(), true);
 681     }
 682 }