author | psandoz |
Fri, 20 Dec 2013 13:38:13 +0100 | |
changeset 22078 | bdec5d53e98c |
parent 20491 | eb2dfc7436af |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
28 |
import java.util.function.Predicate; |
17182 | 29 |
import java.util.stream.Stream; |
30 |
import java.util.stream.StreamSupport; |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
31 |
|
2 | 32 |
/** |
33 |
* The root interface in the <i>collection hierarchy</i>. A collection |
|
34 |
* represents a group of objects, known as its <i>elements</i>. Some |
|
35 |
* collections allow duplicate elements and others do not. Some are ordered |
|
36 |
* and others unordered. The JDK does not provide any <i>direct</i> |
|
37 |
* implementations of this interface: it provides implementations of more |
|
38 |
* specific subinterfaces like <tt>Set</tt> and <tt>List</tt>. This interface |
|
39 |
* is typically used to pass collections around and manipulate them where |
|
40 |
* maximum generality is desired. |
|
41 |
* |
|
42 |
* <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain |
|
43 |
* duplicate elements) should implement this interface directly. |
|
44 |
* |
|
45 |
* <p>All general-purpose <tt>Collection</tt> implementation classes (which |
|
46 |
* typically implement <tt>Collection</tt> indirectly through one of its |
|
47 |
* subinterfaces) should provide two "standard" constructors: a void (no |
|
48 |
* arguments) constructor, which creates an empty collection, and a |
|
49 |
* constructor with a single argument of type <tt>Collection</tt>, which |
|
50 |
* creates a new collection with the same elements as its argument. In |
|
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 <tt>Collection</tt> |
|
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 <tt>UnsupportedOperationException</tt> 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 <tt>UnsupportedOperationException</tt> 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 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18824
diff
changeset
|
67 |
* <p><a name="optional-restrictions"> |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
68 |
* Some collection implementations have restrictions on the elements that |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18824
diff
changeset
|
69 |
* they may contain.</a> For example, some implementations prohibit null elements, |
2 | 70 |
* and some have restrictions on the types of their elements. Attempting to |
71 |
* add an ineligible element throws an unchecked exception, typically |
|
72 |
* <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. 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 <tt>true</tt> if and only if this collection |
|
94 |
* contains at least one element <tt>e</tt> such that |
|
95 |
* <tt>(o==null ? e==null : o.equals(e))</tt>." This specification should |
|
96 |
* <i>not</i> be construed to imply that invoking <tt>Collection.contains</tt> |
|
97 |
* with a non-null argument <tt>o</tt> will cause <tt>o.equals(e)</tt> to be |
|
98 |
* invoked for any element <tt>e</tt>. Implementations are free to implement |
|
99 |
* optimizations whereby the <tt>equals</tt> 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 |
* |
|
20491
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
107 |
* <p>Some collection operations which perform recursive traversal of the |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
108 |
* collection may fail with an exception for self-referential instances where |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
109 |
* the collection directly or indirectly contains itself. This includes the |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
110 |
* {@code clone()}, {@code equals()}, {@code hashCode()} and {@code toString()} |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
111 |
* methods. Implementations may optionally handle the self-referential scenario, |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
112 |
* however most current implementations do not do so. |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20489
diff
changeset
|
113 |
* |
2 | 114 |
* <p>This interface is a member of the |
115 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
116 |
* Java Collections Framework</a>. |
|
117 |
* |
|
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
118 |
* @implSpec |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
119 |
* The default method implementations (inherited or otherwise) do not apply any |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
120 |
* synchronization protocol. If a {@code Collection} implementation has a |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
121 |
* specific synchronization protocol, then it must override default |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
122 |
* implementations to apply that protocol. |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
123 |
* |
4977
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
1818
diff
changeset
|
124 |
* @param <E> the type of elements in this collection |
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
1818
diff
changeset
|
125 |
* |
2 | 126 |
* @author Josh Bloch |
127 |
* @author Neal Gafter |
|
128 |
* @see Set |
|
129 |
* @see List |
|
130 |
* @see Map |
|
131 |
* @see SortedSet |
|
132 |
* @see SortedMap |
|
133 |
* @see HashSet |
|
134 |
* @see TreeSet |
|
135 |
* @see ArrayList |
|
136 |
* @see LinkedList |
|
137 |
* @see Vector |
|
138 |
* @see Collections |
|
139 |
* @see Arrays |
|
140 |
* @see AbstractCollection |
|
141 |
* @since 1.2 |
|
142 |
*/ |
|
143 |
||
144 |
public interface Collection<E> extends Iterable<E> { |
|
145 |
// Query Operations |
|
146 |
||
147 |
/** |
|
148 |
* Returns the number of elements in this collection. If this collection |
|
149 |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
|
150 |
* <tt>Integer.MAX_VALUE</tt>. |
|
151 |
* |
|
152 |
* @return the number of elements in this collection |
|
153 |
*/ |
|
154 |
int size(); |
|
155 |
||
156 |
/** |
|
157 |
* Returns <tt>true</tt> if this collection contains no elements. |
|
158 |
* |
|
159 |
* @return <tt>true</tt> if this collection contains no elements |
|
160 |
*/ |
|
161 |
boolean isEmpty(); |
|
162 |
||
163 |
/** |
|
164 |
* Returns <tt>true</tt> if this collection contains the specified element. |
|
165 |
* More formally, returns <tt>true</tt> if and only if this collection |
|
166 |
* contains at least one element <tt>e</tt> such that |
|
167 |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
|
168 |
* |
|
169 |
* @param o element whose presence in this collection is to be tested |
|
170 |
* @return <tt>true</tt> if this collection contains the specified |
|
171 |
* element |
|
172 |
* @throws ClassCastException if the type of the specified element |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
173 |
* is incompatible with this collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
174 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 175 |
* @throws NullPointerException if the specified element is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
176 |
* collection does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
177 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 178 |
*/ |
179 |
boolean contains(Object o); |
|
180 |
||
181 |
/** |
|
182 |
* Returns an iterator over the elements in this collection. There are no |
|
183 |
* guarantees concerning the order in which the elements are returned |
|
184 |
* (unless this collection is an instance of some class that provides a |
|
185 |
* guarantee). |
|
186 |
* |
|
187 |
* @return an <tt>Iterator</tt> over the elements in this collection |
|
188 |
*/ |
|
189 |
Iterator<E> iterator(); |
|
190 |
||
191 |
/** |
|
192 |
* Returns an array containing all of the elements in this collection. |
|
193 |
* If this collection makes any guarantees as to what order its elements |
|
194 |
* are returned by its iterator, this method must return the elements in |
|
195 |
* the same order. |
|
196 |
* |
|
197 |
* <p>The returned array will be "safe" in that no references to it are |
|
198 |
* maintained by this collection. (In other words, this method must |
|
199 |
* allocate a new array even if this collection is backed by an array). |
|
200 |
* The caller is thus free to modify the returned array. |
|
201 |
* |
|
202 |
* <p>This method acts as bridge between array-based and collection-based |
|
203 |
* APIs. |
|
204 |
* |
|
205 |
* @return an array containing all of the elements in this collection |
|
206 |
*/ |
|
207 |
Object[] toArray(); |
|
208 |
||
209 |
/** |
|
210 |
* Returns an array containing all of the elements in this collection; |
|
211 |
* the runtime type of the returned array is that of the specified array. |
|
212 |
* If the collection fits in the specified array, it is returned therein. |
|
213 |
* Otherwise, a new array is allocated with the runtime type of the |
|
214 |
* specified array and the size of this collection. |
|
215 |
* |
|
216 |
* <p>If this collection fits in the specified array with room to spare |
|
217 |
* (i.e., the array has more elements than this collection), the element |
|
218 |
* in the array immediately following the end of the collection is set to |
|
219 |
* <tt>null</tt>. (This is useful in determining the length of this |
|
220 |
* collection <i>only</i> if the caller knows that this collection does |
|
221 |
* not contain any <tt>null</tt> elements.) |
|
222 |
* |
|
223 |
* <p>If this collection makes any guarantees as to what order its elements |
|
224 |
* are returned by its iterator, this method must return the elements in |
|
225 |
* the same order. |
|
226 |
* |
|
227 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
228 |
* array-based and collection-based APIs. Further, this method allows |
|
229 |
* precise control over the runtime type of the output array, and may, |
|
230 |
* under certain circumstances, be used to save allocation costs. |
|
231 |
* |
|
232 |
* <p>Suppose <tt>x</tt> is a collection known to contain only strings. |
|
233 |
* The following code can be used to dump the collection into a newly |
|
234 |
* allocated array of <tt>String</tt>: |
|
235 |
* |
|
236 |
* <pre> |
|
237 |
* String[] y = x.toArray(new String[0]);</pre> |
|
238 |
* |
|
239 |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
|
240 |
* <tt>toArray()</tt>. |
|
241 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18824
diff
changeset
|
242 |
* @param <T> the runtime type of the array to contain the collection |
2 | 243 |
* @param a the array into which the elements of this collection are to be |
244 |
* stored, if it is big enough; otherwise, a new array of the same |
|
245 |
* runtime type is allocated for this purpose. |
|
246 |
* @return an array containing all of the elements in this collection |
|
247 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
248 |
* is not a supertype of the runtime type of every element in |
|
249 |
* this collection |
|
250 |
* @throws NullPointerException if the specified array is null |
|
251 |
*/ |
|
252 |
<T> T[] toArray(T[] a); |
|
253 |
||
254 |
// Modification Operations |
|
255 |
||
256 |
/** |
|
257 |
* Ensures that this collection contains the specified element (optional |
|
258 |
* operation). Returns <tt>true</tt> if this collection changed as a |
|
259 |
* result of the call. (Returns <tt>false</tt> if this collection does |
|
260 |
* not permit duplicates and already contains the specified element.)<p> |
|
261 |
* |
|
262 |
* Collections that support this operation may place limitations on what |
|
263 |
* elements may be added to this collection. In particular, some |
|
264 |
* collections will refuse to add <tt>null</tt> elements, and others will |
|
265 |
* impose restrictions on the type of elements that may be added. |
|
266 |
* Collection classes should clearly specify in their documentation any |
|
267 |
* restrictions on what elements may be added.<p> |
|
268 |
* |
|
269 |
* If a collection refuses to add a particular element for any reason |
|
270 |
* other than that it already contains the element, it <i>must</i> throw |
|
271 |
* an exception (rather than returning <tt>false</tt>). This preserves |
|
272 |
* the invariant that a collection always contains the specified element |
|
273 |
* after this call returns. |
|
274 |
* |
|
275 |
* @param e element whose presence in this collection is to be ensured |
|
276 |
* @return <tt>true</tt> if this collection changed as a result of the |
|
277 |
* call |
|
278 |
* @throws UnsupportedOperationException if the <tt>add</tt> operation |
|
279 |
* is not supported by this collection |
|
280 |
* @throws ClassCastException if the class of the specified element |
|
281 |
* prevents it from being added to this collection |
|
282 |
* @throws NullPointerException if the specified element is null and this |
|
283 |
* collection does not permit null elements |
|
284 |
* @throws IllegalArgumentException if some property of the element |
|
285 |
* prevents it from being added to this collection |
|
286 |
* @throws IllegalStateException if the element cannot be added at this |
|
287 |
* time due to insertion restrictions |
|
288 |
*/ |
|
289 |
boolean add(E e); |
|
290 |
||
291 |
/** |
|
292 |
* Removes a single instance of the specified element from this |
|
293 |
* collection, if it is present (optional operation). More formally, |
|
294 |
* removes an element <tt>e</tt> such that |
|
295 |
* <tt>(o==null ? e==null : o.equals(e))</tt>, if |
|
296 |
* this collection contains one or more such elements. Returns |
|
297 |
* <tt>true</tt> if this collection contained the specified element (or |
|
298 |
* equivalently, if this collection changed as a result of the call). |
|
299 |
* |
|
300 |
* @param o element to be removed from this collection, if present |
|
301 |
* @return <tt>true</tt> if an element was removed as a result of this call |
|
302 |
* @throws ClassCastException if the type of the specified element |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
303 |
* is incompatible with this collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
304 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 305 |
* @throws NullPointerException if the specified element is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
306 |
* collection does not permit null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
307 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 308 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
309 |
* is not supported by this collection |
|
310 |
*/ |
|
311 |
boolean remove(Object o); |
|
312 |
||
313 |
||
314 |
// Bulk Operations |
|
315 |
||
316 |
/** |
|
317 |
* Returns <tt>true</tt> if this collection contains all of the elements |
|
318 |
* in the specified collection. |
|
319 |
* |
|
320 |
* @param c collection to be checked for containment in this collection |
|
321 |
* @return <tt>true</tt> if this collection contains all of the elements |
|
322 |
* in the specified collection |
|
323 |
* @throws ClassCastException if the types of one or more elements |
|
324 |
* in the specified collection are incompatible with this |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
325 |
* collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
326 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 327 |
* @throws NullPointerException if the specified collection contains one |
328 |
* or more null elements and this collection does not permit null |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
329 |
* elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
330 |
* (<a href="#optional-restrictions">optional</a>), |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
331 |
* or if the specified collection is null. |
2 | 332 |
* @see #contains(Object) |
333 |
*/ |
|
334 |
boolean containsAll(Collection<?> c); |
|
335 |
||
336 |
/** |
|
337 |
* Adds all of the elements in the specified collection to this collection |
|
338 |
* (optional operation). The behavior of this operation is undefined if |
|
339 |
* the specified collection is modified while the operation is in progress. |
|
340 |
* (This implies that the behavior of this call is undefined if the |
|
341 |
* specified collection is this collection, and this collection is |
|
342 |
* nonempty.) |
|
343 |
* |
|
344 |
* @param c collection containing elements to be added to this collection |
|
345 |
* @return <tt>true</tt> if this collection changed as a result of the call |
|
346 |
* @throws UnsupportedOperationException if the <tt>addAll</tt> operation |
|
347 |
* is not supported by this collection |
|
348 |
* @throws ClassCastException if the class of an element of the specified |
|
349 |
* collection prevents it from being added to this collection |
|
350 |
* @throws NullPointerException if the specified collection contains a |
|
351 |
* null element and this collection does not permit null elements, |
|
352 |
* or if the specified collection is null |
|
353 |
* @throws IllegalArgumentException if some property of an element of the |
|
354 |
* specified collection prevents it from being added to this |
|
355 |
* collection |
|
356 |
* @throws IllegalStateException if not all the elements can be added at |
|
357 |
* this time due to insertion restrictions |
|
358 |
* @see #add(Object) |
|
359 |
*/ |
|
360 |
boolean addAll(Collection<? extends E> c); |
|
361 |
||
362 |
/** |
|
363 |
* Removes all of this collection's elements that are also contained in the |
|
364 |
* specified collection (optional operation). After this call returns, |
|
365 |
* this collection will contain no elements in common with the specified |
|
366 |
* collection. |
|
367 |
* |
|
368 |
* @param c collection containing elements to be removed from this collection |
|
369 |
* @return <tt>true</tt> if this collection changed as a result of the |
|
370 |
* call |
|
371 |
* @throws UnsupportedOperationException if the <tt>removeAll</tt> method |
|
372 |
* is not supported by this collection |
|
373 |
* @throws ClassCastException if the types of one or more elements |
|
374 |
* in this collection are incompatible with the specified |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
375 |
* collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
376 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 377 |
* @throws NullPointerException if this collection contains one or more |
378 |
* null elements and the specified collection does not support |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
379 |
* null elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
380 |
* (<a href="#optional-restrictions">optional</a>), |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
381 |
* or if the specified collection is null |
2 | 382 |
* @see #remove(Object) |
383 |
* @see #contains(Object) |
|
384 |
*/ |
|
385 |
boolean removeAll(Collection<?> c); |
|
386 |
||
387 |
/** |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
388 |
* Removes all of the elements of this collection that satisfy the given |
20476
2d01671506ba
8023339: Refined Collection.removeIf UOE conditions
mduigou
parents:
19850
diff
changeset
|
389 |
* predicate. Errors or runtime exceptions thrown during iteration or by |
2d01671506ba
8023339: Refined Collection.removeIf UOE conditions
mduigou
parents:
19850
diff
changeset
|
390 |
* the predicate are relayed to the caller. |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
391 |
* |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
392 |
* @implSpec |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
393 |
* The default implementation traverses all elements of the collection using |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
394 |
* its {@link #iterator}. Each matching element is removed using |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
395 |
* {@link Iterator#remove()}. If the collection's iterator does not |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
396 |
* support removal then an {@code UnsupportedOperationException} will be |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
397 |
* thrown on the first matching element. |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
398 |
* |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
399 |
* @param filter a predicate which returns {@code true} for elements to be |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
400 |
* removed |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
401 |
* @return {@code true} if any elements were removed |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
402 |
* @throws NullPointerException if the specified filter is null |
20476
2d01671506ba
8023339: Refined Collection.removeIf UOE conditions
mduigou
parents:
19850
diff
changeset
|
403 |
* @throws UnsupportedOperationException if elements cannot be removed |
2d01671506ba
8023339: Refined Collection.removeIf UOE conditions
mduigou
parents:
19850
diff
changeset
|
404 |
* from this collection. Implementations may throw this exception if a |
2d01671506ba
8023339: Refined Collection.removeIf UOE conditions
mduigou
parents:
19850
diff
changeset
|
405 |
* matching element cannot be removed or if, in general, removal is not |
2d01671506ba
8023339: Refined Collection.removeIf UOE conditions
mduigou
parents:
19850
diff
changeset
|
406 |
* supported. |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
407 |
* @since 1.8 |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
408 |
*/ |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
409 |
default boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
410 |
Objects.requireNonNull(filter); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
411 |
boolean removed = false; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
412 |
final Iterator<E> each = iterator(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
413 |
while (each.hasNext()) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
414 |
if (filter.test(each.next())) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
415 |
each.remove(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
416 |
removed = true; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
417 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
418 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
419 |
return removed; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
420 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
421 |
|
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16929
diff
changeset
|
422 |
/** |
2 | 423 |
* Retains only the elements in this collection that are contained in the |
424 |
* specified collection (optional operation). In other words, removes from |
|
425 |
* this collection all of its elements that are not contained in the |
|
426 |
* specified collection. |
|
427 |
* |
|
428 |
* @param c collection containing elements to be retained in this collection |
|
429 |
* @return <tt>true</tt> if this collection changed as a result of the call |
|
430 |
* @throws UnsupportedOperationException if the <tt>retainAll</tt> operation |
|
431 |
* is not supported by this collection |
|
432 |
* @throws ClassCastException if the types of one or more elements |
|
433 |
* in this collection are incompatible with the specified |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
434 |
* collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
435 |
* (<a href="#optional-restrictions">optional</a>) |
2 | 436 |
* @throws NullPointerException if this collection contains one or more |
437 |
* null elements and the specified collection does not permit null |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
438 |
* elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
439 |
* (<a href="#optional-restrictions">optional</a>), |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
7668
diff
changeset
|
440 |
* or if the specified collection is null |
2 | 441 |
* @see #remove(Object) |
442 |
* @see #contains(Object) |
|
443 |
*/ |
|
444 |
boolean retainAll(Collection<?> c); |
|
445 |
||
446 |
/** |
|
447 |
* Removes all of the elements from this collection (optional operation). |
|
448 |
* The collection will be empty after this method returns. |
|
449 |
* |
|
450 |
* @throws UnsupportedOperationException if the <tt>clear</tt> operation |
|
451 |
* is not supported by this collection |
|
452 |
*/ |
|
453 |
void clear(); |
|
454 |
||
455 |
||
456 |
// Comparison and hashing |
|
457 |
||
458 |
/** |
|
459 |
* Compares the specified object with this collection for equality. <p> |
|
460 |
* |
|
461 |
* While the <tt>Collection</tt> interface adds no stipulations to the |
|
462 |
* general contract for the <tt>Object.equals</tt>, programmers who |
|
463 |
* implement the <tt>Collection</tt> interface "directly" (in other words, |
|
464 |
* create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt> |
|
465 |
* or a <tt>List</tt>) must exercise care if they choose to override the |
|
466 |
* <tt>Object.equals</tt>. It is not necessary to do so, and the simplest |
|
467 |
* course of action is to rely on <tt>Object</tt>'s implementation, but |
|
468 |
* the implementor may wish to implement a "value comparison" in place of |
|
469 |
* the default "reference comparison." (The <tt>List</tt> and |
|
470 |
* <tt>Set</tt> interfaces mandate such value comparisons.)<p> |
|
471 |
* |
|
472 |
* The general contract for the <tt>Object.equals</tt> method states that |
|
473 |
* equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and |
|
474 |
* only if <tt>b.equals(a)</tt>). The contracts for <tt>List.equals</tt> |
|
475 |
* and <tt>Set.equals</tt> state that lists are only equal to other lists, |
|
476 |
* and sets to other sets. Thus, a custom <tt>equals</tt> method for a |
|
477 |
* collection class that implements neither the <tt>List</tt> nor |
|
478 |
* <tt>Set</tt> interface must return <tt>false</tt> when this collection |
|
479 |
* is compared to any list or set. (By the same logic, it is not possible |
|
480 |
* to write a class that correctly implements both the <tt>Set</tt> and |
|
481 |
* <tt>List</tt> interfaces.) |
|
482 |
* |
|
483 |
* @param o object to be compared for equality with this collection |
|
484 |
* @return <tt>true</tt> if the specified object is equal to this |
|
485 |
* collection |
|
486 |
* |
|
487 |
* @see Object#equals(Object) |
|
488 |
* @see Set#equals(Object) |
|
489 |
* @see List#equals(Object) |
|
490 |
*/ |
|
491 |
boolean equals(Object o); |
|
492 |
||
493 |
/** |
|
494 |
* Returns the hash code value for this collection. While the |
|
495 |
* <tt>Collection</tt> interface adds no stipulations to the general |
|
496 |
* contract for the <tt>Object.hashCode</tt> method, programmers should |
|
497 |
* take note that any class that overrides the <tt>Object.equals</tt> |
|
498 |
* method must also override the <tt>Object.hashCode</tt> method in order |
|
1818 | 499 |
* to satisfy the general contract for the <tt>Object.hashCode</tt> method. |
2 | 500 |
* In particular, <tt>c1.equals(c2)</tt> implies that |
501 |
* <tt>c1.hashCode()==c2.hashCode()</tt>. |
|
502 |
* |
|
503 |
* @return the hash code value for this collection |
|
504 |
* |
|
505 |
* @see Object#hashCode() |
|
506 |
* @see Object#equals(Object) |
|
507 |
*/ |
|
508 |
int hashCode(); |
|
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
509 |
|
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
510 |
/** |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
511 |
* Creates a {@link Spliterator} over the elements in this collection. |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
512 |
* |
20489
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
513 |
* Implementations should document characteristic values reported by the |
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
514 |
* spliterator. Such characteristic values are not required to be reported |
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
515 |
* if the spliterator reports {@link Spliterator#SIZED} and this collection |
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
516 |
* contains no elements. |
17182 | 517 |
* |
518 |
* <p>The default implementation should be overridden by subclasses that |
|
519 |
* can return a more efficient spliterator. In order to |
|
520 |
* preserve expected laziness behavior for the {@link #stream()} and |
|
521 |
* {@link #parallelStream()}} methods, spliterators should either have the |
|
522 |
* characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be |
|
523 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>. |
|
524 |
* If none of these is practical, the overriding class should describe the |
|
525 |
* spliterator's documented policy of binding and structural interference, |
|
526 |
* and should override the {@link #stream()} and {@link #parallelStream()} |
|
527 |
* methods to create streams using a {@code Supplier} of the spliterator, |
|
528 |
* as in: |
|
529 |
* <pre>{@code |
|
530 |
* Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics) |
|
531 |
* }</pre> |
|
532 |
* <p>These requirements ensure that streams produced by the |
|
533 |
* {@link #stream()} and {@link #parallelStream()} methods will reflect the |
|
534 |
* contents of the collection as of initiation of the terminal stream |
|
535 |
* operation. |
|
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
536 |
* |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
537 |
* @implSpec |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
538 |
* The default implementation creates a |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
539 |
* <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
540 |
* from the collections's {@code Iterator}. The spliterator inherits the |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
541 |
* <em>fail-fast</em> properties of the collection's iterator. |
20489
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
542 |
* <p> |
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
543 |
* The created {@code Spliterator} reports {@link Spliterator#SIZED}. |
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
544 |
* |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
545 |
* @implNote |
20489
cce02e4a6cbe
8024408: Specifications for Collection/List/Set/SortedSet.spliterator() need to document if all the (subclass) instances are required to return SIZED spliterators
psandoz
parents:
20476
diff
changeset
|
546 |
* The created {@code Spliterator} additionally reports |
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
547 |
* {@link Spliterator#SUBSIZED}. |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
548 |
* |
19424
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
549 |
* <p>If a spliterator covers no elements then the reporting of additional |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
550 |
* characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED}, |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
551 |
* does not aid clients to control, specialize or simplify computation. |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
552 |
* However, this does enable shared use of an immutable and empty |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
553 |
* spliterator instance (see {@link Spliterators#emptySpliterator()}) for |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
554 |
* empty collections, and enables clients to determine if such a spliterator |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
555 |
* covers no elements. |
c4524285927f
8022797: Clarify spliterator characteristics for collections containing no elements
psandoz
parents:
18829
diff
changeset
|
556 |
* |
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
557 |
* @return a {@code Spliterator} over the elements in this collection |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
558 |
* @since 1.8 |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
559 |
*/ |
19850 | 560 |
@Override |
16929
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
561 |
default Spliterator<E> spliterator() { |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
562 |
return Spliterators.spliterator(this, 0); |
c984ae5655cb
8010096: Initial java.util.Spliterator putback
briangoetz
parents:
14342
diff
changeset
|
563 |
} |
17182 | 564 |
|
565 |
/** |
|
566 |
* Returns a sequential {@code Stream} with this collection as its source. |
|
567 |
* |
|
568 |
* <p>This method should be overridden when the {@link #spliterator()} |
|
569 |
* method cannot return a spliterator that is {@code IMMUTABLE}, |
|
570 |
* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} |
|
571 |
* for details.) |
|
572 |
* |
|
573 |
* @implSpec |
|
574 |
* The default implementation creates a sequential {@code Stream} from the |
|
575 |
* collection's {@code Spliterator}. |
|
576 |
* |
|
577 |
* @return a sequential {@code Stream} over the elements in this collection |
|
578 |
* @since 1.8 |
|
579 |
*/ |
|
580 |
default Stream<E> stream() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
17182
diff
changeset
|
581 |
return StreamSupport.stream(spliterator(), false); |
17182 | 582 |
} |
583 |
||
584 |
/** |
|
585 |
* Returns a possibly parallel {@code Stream} with this collection as its |
|
586 |
* source. It is allowable for this method to return a sequential stream. |
|
587 |
* |
|
588 |
* <p>This method should be overridden when the {@link #spliterator()} |
|
589 |
* method cannot return a spliterator that is {@code IMMUTABLE}, |
|
590 |
* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} |
|
591 |
* for details.) |
|
592 |
* |
|
593 |
* @implSpec |
|
594 |
* The default implementation creates a parallel {@code Stream} from the |
|
595 |
* collection's {@code Spliterator}. |
|
596 |
* |
|
597 |
* @return a possibly parallel {@code Stream} over the elements in this |
|
598 |
* collection |
|
599 |
* @since 1.8 |
|
600 |
*/ |
|
601 |
default Stream<E> parallelStream() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
17182
diff
changeset
|
602 |
return StreamSupport.stream(spliterator(), true); |
17182 | 603 |
} |
2 | 604 |
} |