Path: blob/master/src/java.base/share/classes/java/util/Collection.java
41152 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627import java.util.function.IntFunction;28import java.util.function.Predicate;29import java.util.stream.Stream;30import java.util.stream.StreamSupport;3132/**33* The root interface in the <i>collection hierarchy</i>. A collection34* represents a group of objects, known as its <i>elements</i>. Some35* collections allow duplicate elements and others do not. Some are ordered36* and others unordered. The JDK does not provide any <i>direct</i>37* implementations of this interface: it provides implementations of more38* specific subinterfaces like {@code Set} and {@code List}. This interface39* is typically used to pass collections around and manipulate them where40* maximum generality is desired.41*42* <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain43* duplicate elements) should implement this interface directly.44*45* <p>All general-purpose {@code Collection} implementation classes (which46* typically implement {@code Collection} indirectly through one of its47* subinterfaces) should provide two "standard" constructors: a void (no48* arguments) constructor, which creates an empty collection, and a49* constructor with a single argument of type {@code Collection}, which50* creates a new collection with the same elements as its argument. In51* 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 contain54* 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 be58* <i>optional</i>. If a collection implementation doesn't implement a59* particular operation, it should define the corresponding method to throw60* {@code UnsupportedOperationException}. Such methods are marked "optional61* operation" in method specifications of the collections interfaces.62*63* <p><a id="optional-restrictions"></a>Some collection implementations64* 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 to67* add an ineligible element throws an unchecked exception, typically68* {@code NullPointerException} or {@code ClassCastException}. Attempting69* to query the presence of an ineligible element may throw an exception,70* or it may simply return false; some implementations will exhibit the former71* behavior and some will exhibit the latter. More generally, attempting an72* operation on an ineligible element whose completion would not result in73* the insertion of an ineligible element into the collection may throw an74* exception or it may succeed, at the option of the implementation.75* Such exceptions are marked as "optional" in the specification for this76* interface.77*78* <p>It is up to each collection to determine its own synchronization79* policy. In the absence of a stronger guarantee by the80* implementation, undefined behavior may result from the invocation81* of any method on a collection that is being mutated by another82* thread; this includes direct invocations, passing the collection to83* a method that might perform invocations, and using an existing84* iterator to examine the collection.85*86* <p>Many methods in Collections Framework interfaces are defined in87* 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 collection90* contains at least one element {@code e} such that91* {@code (o==null ? e==null : o.equals(e))}." This specification should92* <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 be94* invoked for any element {@code e}. Implementations are free to implement95* optimizations whereby the {@code equals} invocation is avoided, for96* example, by first comparing the hash codes of the two elements. (The97* {@link Object#hashCode()} specification guarantees that two objects with98* unequal hash codes cannot be equal.) More generally, implementations of99* the various Collections Framework interfaces are free to take advantage of100* the specified behavior of underlying {@link Object} methods wherever the101* implementor deems it appropriate.102*103* <p>Some collection operations which perform recursive traversal of the104* collection may fail with an exception for self-referential instances where105* the collection directly or indirectly contains itself. This includes the106* {@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>view113* collections</i> themselves do not store elements, but instead they rely on a114* backing collection to store the actual elements. Operations that are not handled115* by the view collection itself are delegated to the backing collection. Examples of116* view collections include the wrapper collections returned by methods such as117* {@link Collections#checkedCollection Collections.checkedCollection},118* {@link Collections#synchronizedCollection Collections.synchronizedCollection}, and119* {@link Collections#unmodifiableCollection Collections.unmodifiableCollection}.120* Other examples of view collections include collections that provide a121* different representation of the same elements, for example, as122* provided by {@link List#subList List.subList},123* {@link NavigableSet#subSet NavigableSet.subSet}, or124* {@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 — if changes127* are permitted — are written through to the backing collection.128* Although they technically aren't collections, instances of129* {@link Iterator} and {@link ListIterator} can also allow modifications130* to be written through to the backing collection, and in some cases,131* modifications to the backing collection will be visible to the Iterator132* 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 called137* "mutator" methods in that they modify the group of objects contained within138* the collection on which they operate. They can be specified to throw139* {@code UnsupportedOperationException} if this collection implementation140* does not support the operation. Such methods should (but are not required141* to) throw an {@code UnsupportedOperationException} if the invocation would142* have no effect on the collection. For example, consider a collection that143* does not support the {@link #add add} operation. What will happen if the144* {@link #addAll addAll} method is invoked on this collection, with an empty145* 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 throw147* an exception. However, it is recommended that such cases throw an exception148* unconditionally, as throwing only in certain cases can lead to149* programming errors.150*151* <p>An <i>unmodifiable collection</i> is a collection, all of whose152* mutator methods (as defined above) are specified to throw153* {@code UnsupportedOperationException}. Such a collection thus cannot be154* modified by calling any methods on it. For a collection to be properly155* unmodifiable, any view collections derived from it must also be unmodifiable.156* For example, if a List is unmodifiable, the List returned by157* {@link List#subList List.subList} is also unmodifiable.158*159* <p>An unmodifiable collection is not necessarily immutable. If the160* contained elements are mutable, the entire collection is clearly161* mutable, even though it might be unmodifiable. For example, consider162* two unmodifiable lists containing mutable elements. The result of calling163* {@code list1.equals(list2)} might differ from one call to the next if164* 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 unmodifiable171* and that is also a view onto a backing collection. Its mutator methods throw172* {@code UnsupportedOperationException}, as described above, while173* 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 to176* an internal collection, while preventing them from modifying such177* collections unexpectedly. Examples of unmodifiable view collections178* are those returned by the179* {@link Collections#unmodifiableCollection Collections.unmodifiableCollection},180* {@link Collections#unmodifiableList Collections.unmodifiableList}, and181* 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 an188* unmodifiable view, the view can be considered effectively immutable.189*190* <h2><a id="serializable">Serializability of Collections</a></h2>191*192* <p>Serializability of collections is optional. As such, none of the collections193* interfaces are declared to implement the {@link java.io.Serializable} interface.194* However, serializability is regarded as being generally useful, so most collection195* implementations are serializable.196*197* <p>The collection implementations that are public classes (such as {@code ArrayList}198* or {@code HashMap}) are declared to implement the {@code Serializable} interface if they199* are in fact serializable. Some collections implementations are not public classes,200* such as the <a href="#unmodifiable">unmodifiable collections.</a> In such cases, the201* serializability of such collections is described in the specification of the method202* that creates them, or in some other suitable place. In cases where the serializability203* of a collection is not specified, there is no guarantee about the serializability of such204* collections. In particular, many <a href="#view">view collections</a> are not serializable.205*206* <p>A collection implementation that implements the {@code Serializable} interface cannot207* be guaranteed to be serializable. The reason is that in general, collections208* contain elements of other types, and it is not possible to determine statically209* whether instances of some element type are actually serializable. For example, consider210* a serializable {@code Collection<E>}, where {@code E} does not implement the211* {@code Serializable} interface. The collection may be serializable, if it contains only212* elements of some serializable subtype of {@code E}, or if it is empty. Collections are213* thus said to be <i>conditionally serializable,</i> as the serializability of the collection214* as a whole depends on whether the collection itself is serializable and on whether all215* contained elements are also serializable.216*217* <p>An additional case occurs with instances of {@link SortedSet} and {@link SortedMap}.218* These collections can be created with a {@link Comparator} that imposes an ordering on219* the set elements or map keys. Such a collection is serializable only if the provided220* {@code Comparator} is also serializable.221*222* <p>This interface is a member of the223* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">224* Java Collections Framework</a>.225*226* @implSpec227* The default method implementations (inherited or otherwise) do not apply any228* synchronization protocol. If a {@code Collection} implementation has a229* specific synchronization protocol, then it must override default230* implementations to apply that protocol.231*232* @param <E> the type of elements in this collection233*234* @author Josh Bloch235* @author Neal Gafter236* @see Set237* @see List238* @see Map239* @see SortedSet240* @see SortedMap241* @see HashSet242* @see TreeSet243* @see ArrayList244* @see LinkedList245* @see Vector246* @see Collections247* @see Arrays248* @see AbstractCollection249* @since 1.2250*/251252public interface Collection<E> extends Iterable<E> {253// Query Operations254255/**256* Returns the number of elements in this collection. If this collection257* contains more than {@code Integer.MAX_VALUE} elements, returns258* {@code Integer.MAX_VALUE}.259*260* @return the number of elements in this collection261*/262int size();263264/**265* Returns {@code true} if this collection contains no elements.266*267* @return {@code true} if this collection contains no elements268*/269boolean isEmpty();270271/**272* Returns {@code true} if this collection contains the specified element.273* More formally, returns {@code true} if and only if this collection274* contains at least one element {@code e} such that275* {@code Objects.equals(o, e)}.276*277* @param o element whose presence in this collection is to be tested278* @return {@code true} if this collection contains the specified279* element280* @throws ClassCastException if the type of the specified element281* is incompatible with this collection282* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)283* @throws NullPointerException if the specified element is null and this284* collection does not permit null elements285* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)286*/287boolean contains(Object o);288289/**290* Returns an iterator over the elements in this collection. There are no291* guarantees concerning the order in which the elements are returned292* (unless this collection is an instance of some class that provides a293* guarantee).294*295* @return an {@code Iterator} over the elements in this collection296*/297Iterator<E> iterator();298299/**300* Returns an array containing all of the elements in this collection.301* If this collection makes any guarantees as to what order its elements302* are returned by its iterator, this method must return the elements in303* the same order. The returned array's {@linkplain Class#getComponentType304* runtime component type} is {@code Object}.305*306* <p>The returned array will be "safe" in that no references to it are307* maintained by this collection. (In other words, this method must308* allocate a new array even if this collection is backed by an array).309* The caller is thus free to modify the returned array.310*311* @apiNote312* This method acts as a bridge between array-based and collection-based APIs.313* It returns an array whose runtime type is {@code Object[]}.314* Use {@link #toArray(Object[]) toArray(T[])} to reuse an existing315* array, or use {@link #toArray(IntFunction)} to control the runtime type316* of the array.317*318* @return an array, whose {@linkplain Class#getComponentType runtime component319* type} is {@code Object}, containing all of the elements in this collection320*/321Object[] toArray();322323/**324* Returns an array containing all of the elements in this collection;325* the runtime type of the returned array is that of the specified array.326* If the collection fits in the specified array, it is returned therein.327* Otherwise, a new array is allocated with the runtime type of the328* specified array and the size of this collection.329*330* <p>If this collection fits in the specified array with room to spare331* (i.e., the array has more elements than this collection), the element332* in the array immediately following the end of the collection is set to333* {@code null}. (This is useful in determining the length of this334* collection <i>only</i> if the caller knows that this collection does335* not contain any {@code null} elements.)336*337* <p>If this collection makes any guarantees as to what order its elements338* are returned by its iterator, this method must return the elements in339* the same order.340*341* @apiNote342* This method acts as a bridge between array-based and collection-based APIs.343* It allows an existing array to be reused under certain circumstances.344* Use {@link #toArray()} to create an array whose runtime type is {@code Object[]},345* or use {@link #toArray(IntFunction)} to control the runtime type of346* the array.347*348* <p>Suppose {@code x} is a collection known to contain only strings.349* The following code can be used to dump the collection into a previously350* allocated {@code String} array:351*352* <pre>353* String[] y = new String[SIZE];354* ...355* y = x.toArray(y);</pre>356*357* <p>The return value is reassigned to the variable {@code y}, because a358* new array will be allocated and returned if the collection {@code x} has359* too many elements to fit into the existing array {@code y}.360*361* <p>Note that {@code toArray(new Object[0])} is identical in function to362* {@code toArray()}.363*364* @param <T> the component type of the array to contain the collection365* @param a the array into which the elements of this collection are to be366* stored, if it is big enough; otherwise, a new array of the same367* runtime type is allocated for this purpose.368* @return an array containing all of the elements in this collection369* @throws ArrayStoreException if the runtime type of any element in this370* collection is not assignable to the {@linkplain Class#getComponentType371* runtime component type} of the specified array372* @throws NullPointerException if the specified array is null373*/374<T> T[] toArray(T[] a);375376/**377* Returns an array containing all of the elements in this collection,378* using the provided {@code generator} function to allocate the returned array.379*380* <p>If this collection makes any guarantees as to what order its elements381* are returned by its iterator, this method must return the elements in382* the same order.383*384* @apiNote385* This method acts as a bridge between array-based and collection-based APIs.386* It allows creation of an array of a particular runtime type. Use387* {@link #toArray()} to create an array whose runtime type is {@code Object[]},388* or use {@link #toArray(Object[]) toArray(T[])} to reuse an existing array.389*390* <p>Suppose {@code x} is a collection known to contain only strings.391* The following code can be used to dump the collection into a newly392* allocated array of {@code String}:393*394* <pre>395* String[] y = x.toArray(String[]::new);</pre>396*397* @implSpec398* The default implementation calls the generator function with zero399* and then passes the resulting array to {@link #toArray(Object[]) toArray(T[])}.400*401* @param <T> the component type of the array to contain the collection402* @param generator a function which produces a new array of the desired403* type and the provided length404* @return an array containing all of the elements in this collection405* @throws ArrayStoreException if the runtime type of any element in this406* collection is not assignable to the {@linkplain Class#getComponentType407* runtime component type} of the generated array408* @throws NullPointerException if the generator function is null409* @since 11410*/411default <T> T[] toArray(IntFunction<T[]> generator) {412return toArray(generator.apply(0));413}414415// Modification Operations416417/**418* Ensures that this collection contains the specified element (optional419* operation). Returns {@code true} if this collection changed as a420* result of the call. (Returns {@code false} if this collection does421* not permit duplicates and already contains the specified element.)<p>422*423* Collections that support this operation may place limitations on what424* elements may be added to this collection. In particular, some425* collections will refuse to add {@code null} elements, and others will426* impose restrictions on the type of elements that may be added.427* Collection classes should clearly specify in their documentation any428* restrictions on what elements may be added.<p>429*430* If a collection refuses to add a particular element for any reason431* other than that it already contains the element, it <i>must</i> throw432* an exception (rather than returning {@code false}). This preserves433* the invariant that a collection always contains the specified element434* after this call returns.435*436* @param e element whose presence in this collection is to be ensured437* @return {@code true} if this collection changed as a result of the438* call439* @throws UnsupportedOperationException if the {@code add} operation440* is not supported by this collection441* @throws ClassCastException if the class of the specified element442* prevents it from being added to this collection443* @throws NullPointerException if the specified element is null and this444* collection does not permit null elements445* @throws IllegalArgumentException if some property of the element446* prevents it from being added to this collection447* @throws IllegalStateException if the element cannot be added at this448* time due to insertion restrictions449*/450boolean add(E e);451452/**453* Removes a single instance of the specified element from this454* collection, if it is present (optional operation). More formally,455* removes an element {@code e} such that456* {@code Objects.equals(o, e)}, if457* this collection contains one or more such elements. Returns458* {@code true} if this collection contained the specified element (or459* equivalently, if this collection changed as a result of the call).460*461* @param o element to be removed from this collection, if present462* @return {@code true} if an element was removed as a result of this call463* @throws ClassCastException if the type of the specified element464* is incompatible with this collection465* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)466* @throws NullPointerException if the specified element is null and this467* collection does not permit null elements468* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)469* @throws UnsupportedOperationException if the {@code remove} operation470* is not supported by this collection471*/472boolean remove(Object o);473474475// Bulk Operations476477/**478* Returns {@code true} if this collection contains all of the elements479* in the specified collection.480*481* @param c collection to be checked for containment in this collection482* @return {@code true} if this collection contains all of the elements483* in the specified collection484* @throws ClassCastException if the types of one or more elements485* in the specified collection are incompatible with this486* collection487* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)488* @throws NullPointerException if the specified collection contains one489* or more null elements and this collection does not permit null490* elements491* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),492* or if the specified collection is null.493* @see #contains(Object)494*/495boolean containsAll(Collection<?> c);496497/**498* Adds all of the elements in the specified collection to this collection499* (optional operation). The behavior of this operation is undefined if500* the specified collection is modified while the operation is in progress.501* (This implies that the behavior of this call is undefined if the502* specified collection is this collection, and this collection is503* nonempty.)504*505* @param c collection containing elements to be added to this collection506* @return {@code true} if this collection changed as a result of the call507* @throws UnsupportedOperationException if the {@code addAll} operation508* is not supported by this collection509* @throws ClassCastException if the class of an element of the specified510* collection prevents it from being added to this collection511* @throws NullPointerException if the specified collection contains a512* null element and this collection does not permit null elements,513* or if the specified collection is null514* @throws IllegalArgumentException if some property of an element of the515* specified collection prevents it from being added to this516* collection517* @throws IllegalStateException if not all the elements can be added at518* this time due to insertion restrictions519* @see #add(Object)520*/521boolean addAll(Collection<? extends E> c);522523/**524* Removes all of this collection's elements that are also contained in the525* specified collection (optional operation). After this call returns,526* this collection will contain no elements in common with the specified527* collection.528*529* @param c collection containing elements to be removed from this collection530* @return {@code true} if this collection changed as a result of the531* call532* @throws UnsupportedOperationException if the {@code removeAll} method533* is not supported by this collection534* @throws ClassCastException if the types of one or more elements535* in this collection are incompatible with the specified536* collection537* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)538* @throws NullPointerException if this collection contains one or more539* null elements and the specified collection does not support540* null elements541* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),542* or if the specified collection is null543* @see #remove(Object)544* @see #contains(Object)545*/546boolean removeAll(Collection<?> c);547548/**549* Removes all of the elements of this collection that satisfy the given550* predicate. Errors or runtime exceptions thrown during iteration or by551* the predicate are relayed to the caller.552*553* @implSpec554* The default implementation traverses all elements of the collection using555* its {@link #iterator}. Each matching element is removed using556* {@link Iterator#remove()}. If the collection's iterator does not557* support removal then an {@code UnsupportedOperationException} will be558* thrown on the first matching element.559*560* @param filter a predicate which returns {@code true} for elements to be561* removed562* @return {@code true} if any elements were removed563* @throws NullPointerException if the specified filter is null564* @throws UnsupportedOperationException if elements cannot be removed565* from this collection. Implementations may throw this exception if a566* matching element cannot be removed or if, in general, removal is not567* supported.568* @since 1.8569*/570default boolean removeIf(Predicate<? super E> filter) {571Objects.requireNonNull(filter);572boolean removed = false;573final Iterator<E> each = iterator();574while (each.hasNext()) {575if (filter.test(each.next())) {576each.remove();577removed = true;578}579}580return removed;581}582583/**584* Retains only the elements in this collection that are contained in the585* specified collection (optional operation). In other words, removes from586* this collection all of its elements that are not contained in the587* specified collection.588*589* @param c collection containing elements to be retained in this collection590* @return {@code true} if this collection changed as a result of the call591* @throws UnsupportedOperationException if the {@code retainAll} operation592* is not supported by this collection593* @throws ClassCastException if the types of one or more elements594* in this collection are incompatible with the specified595* collection596* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)597* @throws NullPointerException if this collection contains one or more598* null elements and the specified collection does not permit null599* elements600* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),601* or if the specified collection is null602* @see #remove(Object)603* @see #contains(Object)604*/605boolean retainAll(Collection<?> c);606607/**608* Removes all of the elements from this collection (optional operation).609* The collection will be empty after this method returns.610*611* @throws UnsupportedOperationException if the {@code clear} operation612* is not supported by this collection613*/614void clear();615616617// Comparison and hashing618619/**620* Compares the specified object with this collection for equality. <p>621*622* While the {@code Collection} interface adds no stipulations to the623* general contract for the {@code Object.equals}, programmers who624* implement the {@code Collection} interface "directly" (in other words,625* create a class that is a {@code Collection} but is not a {@code Set}626* or a {@code List}) must exercise care if they choose to override the627* {@code Object.equals}. It is not necessary to do so, and the simplest628* course of action is to rely on {@code Object}'s implementation, but629* the implementor may wish to implement a "value comparison" in place of630* the default "reference comparison." (The {@code List} and631* {@code Set} interfaces mandate such value comparisons.)<p>632*633* The general contract for the {@code Object.equals} method states that634* equals must be symmetric (in other words, {@code a.equals(b)} if and635* only if {@code b.equals(a)}). The contracts for {@code List.equals}636* and {@code Set.equals} state that lists are only equal to other lists,637* and sets to other sets. Thus, a custom {@code equals} method for a638* collection class that implements neither the {@code List} nor639* {@code Set} interface must return {@code false} when this collection640* is compared to any list or set. (By the same logic, it is not possible641* to write a class that correctly implements both the {@code Set} and642* {@code List} interfaces.)643*644* @param o object to be compared for equality with this collection645* @return {@code true} if the specified object is equal to this646* collection647*648* @see Object#equals(Object)649* @see Set#equals(Object)650* @see List#equals(Object)651*/652boolean equals(Object o);653654/**655* Returns the hash code value for this collection. While the656* {@code Collection} interface adds no stipulations to the general657* contract for the {@code Object.hashCode} method, programmers should658* take note that any class that overrides the {@code Object.equals}659* method must also override the {@code Object.hashCode} method in order660* to satisfy the general contract for the {@code Object.hashCode} method.661* In particular, {@code c1.equals(c2)} implies that662* {@code c1.hashCode()==c2.hashCode()}.663*664* @return the hash code value for this collection665*666* @see Object#hashCode()667* @see Object#equals(Object)668*/669int hashCode();670671/**672* Creates a {@link Spliterator} over the elements in this collection.673*674* Implementations should document characteristic values reported by the675* spliterator. Such characteristic values are not required to be reported676* if the spliterator reports {@link Spliterator#SIZED} and this collection677* contains no elements.678*679* <p>The default implementation should be overridden by subclasses that680* can return a more efficient spliterator. In order to681* preserve expected laziness behavior for the {@link #stream()} and682* {@link #parallelStream()} methods, spliterators should either have the683* characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be684* <em><a href="Spliterator.html#binding">late-binding</a></em>.685* If none of these is practical, the overriding class should describe the686* spliterator's documented policy of binding and structural interference,687* and should override the {@link #stream()} and {@link #parallelStream()}688* methods to create streams using a {@code Supplier} of the spliterator,689* as in:690* <pre>{@code691* Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics)692* }</pre>693* <p>These requirements ensure that streams produced by the694* {@link #stream()} and {@link #parallelStream()} methods will reflect the695* contents of the collection as of initiation of the terminal stream696* operation.697*698* @implSpec699* The default implementation creates a700* <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator701* from the collection's {@code Iterator}. The spliterator inherits the702* <em>fail-fast</em> properties of the collection's iterator.703* <p>704* The created {@code Spliterator} reports {@link Spliterator#SIZED}.705*706* @implNote707* The created {@code Spliterator} additionally reports708* {@link Spliterator#SUBSIZED}.709*710* <p>If a spliterator covers no elements then the reporting of additional711* characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED},712* does not aid clients to control, specialize or simplify computation.713* However, this does enable shared use of an immutable and empty714* spliterator instance (see {@link Spliterators#emptySpliterator()}) for715* empty collections, and enables clients to determine if such a spliterator716* covers no elements.717*718* @return a {@code Spliterator} over the elements in this collection719* @since 1.8720*/721@Override722default Spliterator<E> spliterator() {723return Spliterators.spliterator(this, 0);724}725726/**727* Returns a sequential {@code Stream} with this collection as its source.728*729* <p>This method should be overridden when the {@link #spliterator()}730* method cannot return a spliterator that is {@code IMMUTABLE},731* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}732* for details.)733*734* @implSpec735* The default implementation creates a sequential {@code Stream} from the736* collection's {@code Spliterator}.737*738* @return a sequential {@code Stream} over the elements in this collection739* @since 1.8740*/741default Stream<E> stream() {742return StreamSupport.stream(spliterator(), false);743}744745/**746* Returns a possibly parallel {@code Stream} with this collection as its747* source. It is allowable for this method to return a sequential stream.748*749* <p>This method should be overridden when the {@link #spliterator()}750* method cannot return a spliterator that is {@code IMMUTABLE},751* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}752* for details.)753*754* @implSpec755* The default implementation creates a parallel {@code Stream} from the756* collection's {@code Spliterator}.757*758* @return a possibly parallel {@code Stream} over the elements in this759* collection760* @since 1.8761*/762default Stream<E> parallelStream() {763return StreamSupport.stream(spliterator(), true);764}765}766767768