       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>The "destructive" methods contained in this interface, that is, the
      58  * methods that modify the collection on which they operate, are specified to
      59  * throw {@code UnsupportedOperationException} if this collection does not
      60  * support the operation.  If this is the case, these methods may, but are not
      61  * required to, throw an {@code UnsupportedOperationException} if the
      62  * invocation would have no effect on the collection.  For example, invoking
      63  * the {@link #addAll(Collection)} method on an unmodifiable collection may,
      64  * but is not required to, throw the exception if the collection to be added
      65  * is empty.
      66  *
      67  * <p><a id="optional-restrictions">
      68  * Some collection implementations have restrictions on the elements that
      69  * they may contain.</a>  For example, some implementations prohibit null elements,
      70  * and some have restrictions on the types of their elements.  Attempting to
      71  * add an ineligible element throws an unchecked exception, typically
      72  * {@code NullPointerException} or {@code ClassCastException}.  Attempting
      73  * to query the presence of an ineligible element may throw an exception,
      74  * or it may simply return false; some implementations will exhibit the former
      75  * behavior and some will exhibit the latter.  More generally, attempting an
      76  * operation on an ineligible element whose completion would not result in
      77  * the insertion of an ineligible element into the collection may throw an
      78  * exception or it may succeed, at the option of the implementation.
      79  * Such exceptions are marked as "optional" in the specification for this
      80  * interface.
      81  *
      82  * <p>It is up to each collection to determine its own synchronization
      83  * policy.  In the absence of a stronger guarantee by the
      84  * implementation, undefined behavior may result from the invocation
      85  * of any method on a collection that is being mutated by another
      86  * thread; this includes direct invocations, passing the collection to
      87  * a method that might perform invocations, and using an existing
      88  * iterator to examine the collection.
      89  *
      90  * <p>Many methods in Collections Framework interfaces are defined in
      91  * terms of the {@link Object#equals(Object) equals} method.  For example,
      92  * the specification for the {@link #contains(Object) contains(Object o)}
      93  * method says: "returns {@code true} if and only if this collection
      94  * contains at least one element {@code e} such that
      95  * {@code (o==null ? e==null : o.equals(e))}."  This specification should
      96  * <i>not</i> be construed to imply that invoking {@code Collection.contains}
      97  * with a non-null argument {@code o} will cause {@code o.equals(e)} to be
      98  * invoked for any element {@code e}.  Implementations are free to implement
      99  * optimizations whereby the {@code equals} invocation is avoided, for
     100  * example, by first comparing the hash codes of the two elements.  (The
     101  * {@link Object#hashCode()} specification guarantees that two objects with
     102  * unequal hash codes cannot be equal.)  More generally, implementations of
     103  * the various Collections Framework interfaces are free to take advantage of
     104  * the specified behavior of underlying {@link Object} methods wherever the
     105  * implementor deems it appropriate.
     106  *
     107  * <p>Some collection operations which perform recursive traversal of the
     108  * collection may fail with an exception for self-referential instances where
     109  * the collection directly or indirectly contains itself. This includes the
     110  * {@code clone()}, {@code equals()}, {@code hashCode()} and {@code toString()}
     111  * methods. Implementations may optionally handle the self-referential scenario,
     112  * however most current implementations do not do so.
     113  *
     114  * <p>This interface is a member of the
     115  * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
     116  * Java Collections Framework</a>.
     117  *
     118  * @implSpec
     119  * The default method implementations (inherited or otherwise) do not apply any
     120  * synchronization protocol.  If a {@code Collection} implementation has a
     121  * specific synchronization protocol, then it must override default
     122  * implementations to apply that protocol.
     123  *
     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 {@code Integer.MAX_VALUE} elements, returns
     150      * {@code Integer.MAX_VALUE}.
     151      *
     152      * @return the number of elements in this collection
     153      */
     154     int size();
     155
     156     /**
     157      * Returns {@code true} if this collection contains no elements.
     158      *
     159      * @return {@code true} if this collection contains no elements
     160      */
     161     boolean isEmpty();
     162
     163     /**
     164      * Returns {@code true} if this collection contains the specified element.
     165      * More formally, returns {@code true} if and only if this collection
     166      * contains at least one element {@code e} such that
     167      * {@code Objects.equals(o, e)}.
     168      *
     169      * @param o element whose presence in this collection is to be tested
     170      * @return {@code true} 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="{@docRoot}/java/util/Collection.html#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="{@docRoot}/java/util/Collection.html#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 {@code Iterator} 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. The returned array's {@linkplain Class#getComponentType
     196      * runtime component type} is {@code Object}.
     197      *
     198      * <p>The returned array will be "safe" in that no references to it are
     199      * maintained by this collection.  (In other words, this method must
     200      * allocate a new array even if this collection is backed by an array).
     201      * The caller is thus free to modify the returned array.
     202      *
     203      * <p>This method acts as bridge between array-based and collection-based
     204      * APIs.
     205      *
     206      * @return an array, whose {@linkplain Class#getComponentType runtime component
     207      * type} is {@code Object}, containing all of the elements in this collection
     208      */
     209     Object[] toArray();
     210
     211     /**
     212      * Returns an array containing all of the elements in this collection;
     213      * the runtime type of the returned array is that of the specified array.
     214      * If the collection fits in the specified array, it is returned therein.
     215      * Otherwise, a new array is allocated with the runtime type of the
     216      * specified array and the size of this collection.
     217      *
     218      * <p>If this collection fits in the specified array with room to spare
     219      * (i.e., the array has more elements than this collection), the element
     220      * in the array immediately following the end of the collection is set to
     221      * {@code null}.  (This is useful in determining the length of this
     222      * collection <i>only</i> if the caller knows that this collection does
     223      * not contain any {@code null} elements.)
     224      *
     225      * <p>If this collection makes any guarantees as to what order its elements
     226      * are returned by its iterator, this method must return the elements in
     227      * the same order.
     228      *
     229      * <p>Like the {@link #toArray()} method, this method acts as bridge between
     230      * array-based and collection-based APIs.  Further, this method allows
     231      * precise control over the runtime type of the output array, and may,
     232      * under certain circumstances, be used to save allocation costs.
     233      *
     234      * <p>Suppose {@code x} is a collection known to contain only strings.
     235      * The following code can be used to dump the collection into a newly
     236      * allocated array of {@code String}:
     237      *
     238      * <pre>
     239      *     String[] y = x.toArray(new String[0]);</pre>
     240      *
     241      * Note that {@code toArray(new Object[0])} is identical in function to
     242      * {@code toArray()}.
     243      *
     244      * @param <T> the component type of the array to contain the collection
     245      * @param a the array into which the elements of this collection are to be
     246      *        stored, if it is big enough; otherwise, a new array of the same
     247      *        runtime type is allocated for this purpose.
     248      * @return an array containing all of the elements in this collection
     249      * @throws ArrayStoreException if the runtime type of any element in this
     250      *         collection is not assignable to the {@linkplain Class#getComponentType
     251      *         runtime component type} of the specified array
     252      * @throws NullPointerException if the specified array is null
     253      */
     254     <T> T[] toArray(T[] a);
     255
     256     // Modification Operations
     257
     258     /**
     259      * Ensures that this collection contains the specified element (optional
     260      * operation).  Returns {@code true} if this collection changed as a
     261      * result of the call.  (Returns {@code false} if this collection does
     262      * not permit duplicates and already contains the specified element.)<p>
     263      *
     264      * Collections that support this operation may place limitations on what
     265      * elements may be added to this collection.  In particular, some
     266      * collections will refuse to add {@code null} elements, and others will
     267      * impose restrictions on the type of elements that may be added.
     268      * Collection classes should clearly specify in their documentation any
     269      * restrictions on what elements may be added.<p>
     270      *
     271      * If a collection refuses to add a particular element for any reason
     272      * other than that it already contains the element, it <i>must</i> throw
     273      * an exception (rather than returning {@code false}).  This preserves
     274      * the invariant that a collection always contains the specified element
     275      * after this call returns.
     276      *
     277      * @param e element whose presence in this collection is to be ensured
     278      * @return {@code true} if this collection changed as a result of the
     279      *         call
     280      * @throws UnsupportedOperationException if the {@code add} operation
     281      *         is not supported by this collection
     282      * @throws ClassCastException if the class of the specified element
     283      *         prevents it from being added to this collection
     284      * @throws NullPointerException if the specified element is null and this
     285      *         collection does not permit null elements
     286      * @throws IllegalArgumentException if some property of the element
     287      *         prevents it from being added to this collection
     288      * @throws IllegalStateException if the element cannot be added at this
     289      *         time due to insertion restrictions
     290      */
     291     boolean add(E e);
     292
     293     /**
     294      * Removes a single instance of the specified element from this
     295      * collection, if it is present (optional operation).  More formally,
     296      * removes an element {@code e} such that
     297      * {@code Objects.equals(o, e)}, if
     298      * this collection contains one or more such elements.  Returns
     299      * {@code true} if this collection contained the specified element (or
     300      * equivalently, if this collection changed as a result of the call).
     301      *
     302      * @param o element to be removed from this collection, if present
     303      * @return {@code true} if an element was removed as a result of this call
     304      * @throws ClassCastException if the type of the specified element
     305      *         is incompatible with this collection
     306      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     307      * @throws NullPointerException if the specified element is null and this
     308      *         collection does not permit null elements
     309      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     310      * @throws UnsupportedOperationException if the {@code remove} operation
     311      *         is not supported by this collection
     312      */
     313     boolean remove(Object o);
     314
     315
     316     // Bulk Operations
     317
     318     /**
     319      * Returns {@code true} if this collection contains all of the elements
     320      * in the specified collection.
     321      *
     322      * @param  c collection to be checked for containment in this collection
     323      * @return {@code true} if this collection contains all of the elements
     324      *         in the specified collection
     325      * @throws ClassCastException if the types of one or more elements
     326      *         in the specified collection are incompatible with this
     327      *         collection
     328      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     329      * @throws NullPointerException if the specified collection contains one
     330      *         or more null elements and this collection does not permit null
     331      *         elements
     332      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
     333      *         or if the specified collection is null.
     334      * @see    #contains(Object)
     335      */
     336     boolean containsAll(Collection<?> c);
     337
     338     /**
     339      * Adds all of the elements in the specified collection to this collection
     340      * (optional operation).  The behavior of this operation is undefined if
     341      * the specified collection is modified while the operation is in progress.
     342      * (This implies that the behavior of this call is undefined if the
     343      * specified collection is this collection, and this collection is
     344      * nonempty.)
     345      *
     346      * @param c collection containing elements to be added to this collection
     347      * @return {@code true} if this collection changed as a result of the call
     348      * @throws UnsupportedOperationException if the {@code addAll} operation
     349      *         is not supported by this collection
     350      * @throws ClassCastException if the class of an element of the specified
     351      *         collection prevents it from being added to this collection
     352      * @throws NullPointerException if the specified collection contains a
     353      *         null element and this collection does not permit null elements,
     354      *         or if the specified collection is null
     355      * @throws IllegalArgumentException if some property of an element of the
     356      *         specified collection prevents it from being added to this
     357      *         collection
     358      * @throws IllegalStateException if not all the elements can be added at
     359      *         this time due to insertion restrictions
     360      * @see #add(Object)
     361      */
     362     boolean addAll(Collection<? extends E> c);
     363
     364     /**
     365      * Removes all of this collection's elements that are also contained in the
     366      * specified collection (optional operation).  After this call returns,
     367      * this collection will contain no elements in common with the specified
     368      * collection.
     369      *
     370      * @param c collection containing elements to be removed from this collection
     371      * @return {@code true} if this collection changed as a result of the
     372      *         call
     373      * @throws UnsupportedOperationException if the {@code removeAll} method
     374      *         is not supported by this collection
     375      * @throws ClassCastException if the types of one or more elements
     376      *         in this collection are incompatible with the specified
     377      *         collection
     378      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     379      * @throws NullPointerException if this collection contains one or more
     380      *         null elements and the specified collection does not support
     381      *         null elements
     382      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
     383      *         or if the specified collection is null
     384      * @see #remove(Object)
     385      * @see #contains(Object)
     386      */
     387     boolean removeAll(Collection<?> c);
     388
     389     /**
     390      * Removes all of the elements of this collection that satisfy the given
     391      * predicate.  Errors or runtime exceptions thrown during iteration or by
     392      * the predicate are relayed to the caller.
     393      *
     394      * @implSpec
     395      * The default implementation traverses all elements of the collection using
     396      * its {@link #iterator}.  Each matching element is removed using
     397      * {@link Iterator#remove()}.  If the collection's iterator does not
     398      * support removal then an {@code UnsupportedOperationException} will be
     399      * thrown on the first matching element.
     400      *
     401      * @param filter a predicate which returns {@code true} for elements to be
     402      *        removed
     403      * @return {@code true} if any elements were removed
     404      * @throws NullPointerException if the specified filter is null
     405      * @throws UnsupportedOperationException if elements cannot be removed
     406      *         from this collection.  Implementations may throw this exception if a
     407      *         matching element cannot be removed or if, in general, removal is not
     408      *         supported.
     409      * @since 1.8
     410      */
     411     default boolean removeIf(Predicate<? super E> filter) {
     412         Objects.requireNonNull(filter);
     413         boolean removed = false;
     414         final Iterator<E> each = iterator();
     415         while (each.hasNext()) {
     416             if (filter.test(each.next())) {
     417                 each.remove();
     418                 removed = true;
     419             }
     420         }
     421         return removed;
     422     }
     423
     424     /**
     425      * Retains only the elements in this collection that are contained in the
     426      * specified collection (optional operation).  In other words, removes from
     427      * this collection all of its elements that are not contained in the
     428      * specified collection.
     429      *
     430      * @param c collection containing elements to be retained in this collection
     431      * @return {@code true} if this collection changed as a result of the call
     432      * @throws UnsupportedOperationException if the {@code retainAll} operation
     433      *         is not supported by this collection
     434      * @throws ClassCastException if the types of one or more elements
     435      *         in this collection are incompatible with the specified
     436      *         collection
     437      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     438      * @throws NullPointerException if this collection contains one or more
     439      *         null elements and the specified collection does not permit null
     440      *         elements
     441      *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>),
     442      *         or if the specified collection is null
     443      * @see #remove(Object)
     444      * @see #contains(Object)
     445      */
     446     boolean retainAll(Collection<?> c);
     447
     448     /**
     449      * Removes all of the elements from this collection (optional operation).
     450      * The collection will be empty after this method returns.
     451      *
     452      * @throws UnsupportedOperationException if the {@code clear} operation
     453      *         is not supported by this collection
     454      */
     455     void clear();
     456
     457
     458     // Comparison and hashing
     459
     460     /**
     461      * Compares the specified object with this collection for equality. <p>
     462      *
     463      * While the {@code Collection} interface adds no stipulations to the
     464      * general contract for the {@code Object.equals}, programmers who
     465      * implement the {@code Collection} interface "directly" (in other words,
     466      * create a class that is a {@code Collection} but is not a {@code Set}
     467      * or a {@code List}) must exercise care if they choose to override the
     468      * {@code Object.equals}.  It is not necessary to do so, and the simplest
     469      * course of action is to rely on {@code Object}'s implementation, but
     470      * the implementor may wish to implement a "value comparison" in place of
     471      * the default "reference comparison."  (The {@code List} and
     472      * {@code Set} interfaces mandate such value comparisons.)<p>
     473      *
     474      * The general contract for the {@code Object.equals} method states that
     475      * equals must be symmetric (in other words, {@code a.equals(b)} if and
     476      * only if {@code b.equals(a)}).  The contracts for {@code List.equals}
     477      * and {@code Set.equals} state that lists are only equal to other lists,
     478      * and sets to other sets.  Thus, a custom {@code equals} method for a
     479      * collection class that implements neither the {@code List} nor
     480      * {@code Set} interface must return {@code false} when this collection
     481      * is compared to any list or set.  (By the same logic, it is not possible
     482      * to write a class that correctly implements both the {@code Set} and
     483      * {@code List} interfaces.)
     484      *
     485      * @param o object to be compared for equality with this collection
     486      * @return {@code true} if the specified object is equal to this
     487      * collection
     488      *
     489      * @see Object#equals(Object)
     490      * @see Set#equals(Object)
     491      * @see List#equals(Object)
     492      */
     493     boolean equals(Object o);
     494
     495     /**
     496      * Returns the hash code value for this collection.  While the
     497      * {@code Collection} interface adds no stipulations to the general
     498      * contract for the {@code Object.hashCode} method, programmers should
     499      * take note that any class that overrides the {@code Object.equals}
     500      * method must also override the {@code Object.hashCode} method in order
     501      * to satisfy the general contract for the {@code Object.hashCode} method.
     502      * In particular, {@code c1.equals(c2)} implies that
     503      * {@code c1.hashCode()==c2.hashCode()}.
     504      *
     505      * @return the hash code value for this collection
     506      *
     507      * @see Object#hashCode()
     508      * @see Object#equals(Object)
     509      */
     510     int hashCode();
     511
     512     /**
     513      * Creates a {@link Spliterator} over the elements in this collection.
     514      *
     515      * Implementations should document characteristic values reported by the
     516      * spliterator.  Such characteristic values are not required to be reported
     517      * if the spliterator reports {@link Spliterator#SIZED} and this collection
     518      * contains no elements.
     519      *
     520      * <p>The default implementation should be overridden by subclasses that
     521      * can return a more efficient spliterator.  In order to
     522      * preserve expected laziness behavior for the {@link #stream()} and
     523      * {@link #parallelStream()} methods, spliterators should either have the
     524      * characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be
     525      * <em><a href="Spliterator.html#binding">late-binding</a></em>.
     526      * If none of these is practical, the overriding class should describe the
     527      * spliterator's documented policy of binding and structural interference,
     528      * and should override the {@link #stream()} and {@link #parallelStream()}
     529      * methods to create streams using a {@code Supplier} of the spliterator,
     530      * as in:
     531      * <pre>{@code
     532      *     Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)
     533      * }</pre>
     534      * <p>These requirements ensure that streams produced by the
     535      * {@link #stream()} and {@link #parallelStream()} methods will reflect the
     536      * contents of the collection as of initiation of the terminal stream
     537      * operation.
     538      *
     539      * @implSpec
     540      * The default implementation creates a
     541      * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator
     542      * from the collection's {@code Iterator}.  The spliterator inherits the
     543      * <em>fail-fast</em> properties of the collection's iterator.
     544      * <p>
     545      * The created {@code Spliterator} reports {@link Spliterator#SIZED}.
     546      *
     547      * @implNote
     548      * The created {@code Spliterator} additionally reports
     549      * {@link Spliterator#SUBSIZED}.
     550      *
     551      * <p>If a spliterator covers no elements then the reporting of additional
     552      * characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},
     553      * does not aid clients to control, specialize or simplify computation.
     554      * However, this does enable shared use of an immutable and empty
     555      * spliterator instance (see {@link Spliterators#emptySpliterator()}) for
     556      * empty collections, and enables clients to determine if such a spliterator
     557      * covers no elements.
     558      *
     559      * @return a {@code Spliterator} over the elements in this collection
     560      * @since 1.8
     561      */
     562     @Override
     563     default Spliterator<E> spliterator() {
     564         return Spliterators.spliterator(this, 0);
     565     }
     566
     567     /**
     568      * Returns a sequential {@code Stream} with this collection as its source.
     569      *
     570      * <p>This method should be overridden when the {@link #spliterator()}
     571      * method cannot return a spliterator that is {@code IMMUTABLE},
     572      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
     573      * for details.)
     574      *
     575      * @implSpec
     576      * The default implementation creates a sequential {@code Stream} from the
     577      * collection's {@code Spliterator}.
     578      *
     579      * @return a sequential {@code Stream} over the elements in this collection
     580      * @since 1.8
     581      */
     582     default Stream<E> stream() {
     583         return StreamSupport.stream(spliterator(), false);
     584     }
     585
     586     /**
     587      * Returns a possibly parallel {@code Stream} with this collection as its
     588      * source.  It is allowable for this method to return a sequential stream.
     589      *
     590      * <p>This method should be overridden when the {@link #spliterator()}
     591      * method cannot return a spliterator that is {@code IMMUTABLE},
     592      * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
     593      * for details.)
     594      *
     595      * @implSpec
     596      * The default implementation creates a parallel {@code Stream} from the
     597      * collection's {@code Spliterator}.
     598      *
     599      * @return a possibly parallel {@code Stream} over the elements in this
     600      * collection
     601      * @since 1.8
     602      */
     603     default Stream<E> parallelStream() {
     604         return StreamSupport.stream(spliterator(), true);
     605     }
     606 }