25 |
25 |
26 package java.util; |
26 package java.util; |
27 |
27 |
28 /** |
28 /** |
29 * A collection that contains no duplicate elements. More formally, sets |
29 * A collection that contains no duplicate elements. More formally, sets |
30 * contain no pair of elements <code>e1</code> and <code>e2</code> such that |
30 * contain no pair of elements {@code e1} and {@code e2} such that |
31 * <code>e1.equals(e2)</code>, and at most one null element. As implied by |
31 * {@code e1.equals(e2)}, and at most one null element. As implied by |
32 * its name, this interface models the mathematical <i>set</i> abstraction. |
32 * its name, this interface models the mathematical <i>set</i> abstraction. |
33 * |
33 * |
34 * <p>The <tt>Set</tt> interface places additional stipulations, beyond those |
34 * <p>The {@code Set} interface places additional stipulations, beyond those |
35 * inherited from the <tt>Collection</tt> interface, on the contracts of all |
35 * inherited from the {@code Collection} interface, on the contracts of all |
36 * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and |
36 * constructors and on the contracts of the {@code add}, {@code equals} and |
37 * <tt>hashCode</tt> methods. Declarations for other inherited methods are |
37 * {@code hashCode} methods. Declarations for other inherited methods are |
38 * also included here for convenience. (The specifications accompanying these |
38 * also included here for convenience. (The specifications accompanying these |
39 * declarations have been tailored to the <tt>Set</tt> interface, but they do |
39 * declarations have been tailored to the {@code Set} interface, but they do |
40 * not contain any additional stipulations.) |
40 * not contain any additional stipulations.) |
41 * |
41 * |
42 * <p>The additional stipulation on constructors is, not surprisingly, |
42 * <p>The additional stipulation on constructors is, not surprisingly, |
43 * that all constructors must create a set that contains no duplicate elements |
43 * that all constructors must create a set that contains no duplicate elements |
44 * (as defined above). |
44 * (as defined above). |
45 * |
45 * |
46 * <p>Note: Great care must be exercised if mutable objects are used as set |
46 * <p>Note: Great care must be exercised if mutable objects are used as set |
47 * elements. The behavior of a set is not specified if the value of an object |
47 * elements. The behavior of a set is not specified if the value of an object |
48 * is changed in a manner that affects <tt>equals</tt> comparisons while the |
48 * is changed in a manner that affects {@code equals} comparisons while the |
49 * object is an element in the set. A special case of this prohibition is |
49 * object is an element in the set. A special case of this prohibition is |
50 * that it is not permissible for a set to contain itself as an element. |
50 * that it is not permissible for a set to contain itself as an element. |
51 * |
51 * |
52 * <p>Some set implementations have restrictions on the elements that |
52 * <p>Some set implementations have restrictions on the elements that |
53 * they may contain. For example, some implementations prohibit null elements, |
53 * they may contain. For example, some implementations prohibit null elements, |
54 * and some have restrictions on the types of their elements. Attempting to |
54 * and some have restrictions on the types of their elements. Attempting to |
55 * add an ineligible element throws an unchecked exception, typically |
55 * add an ineligible element throws an unchecked exception, typically |
56 * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting |
56 * {@code NullPointerException} or {@code ClassCastException}. Attempting |
57 * to query the presence of an ineligible element may throw an exception, |
57 * to query the presence of an ineligible element may throw an exception, |
58 * or it may simply return false; some implementations will exhibit the former |
58 * or it may simply return false; some implementations will exhibit the former |
59 * behavior and some will exhibit the latter. More generally, attempting an |
59 * behavior and some will exhibit the latter. More generally, attempting an |
60 * operation on an ineligible element whose completion would not result in |
60 * operation on an ineligible element whose completion would not result in |
61 * the insertion of an ineligible element into the set may throw an |
61 * the insertion of an ineligible element into the set may throw an |
85 public interface Set<E> extends Collection<E> { |
85 public interface Set<E> extends Collection<E> { |
86 // Query Operations |
86 // Query Operations |
87 |
87 |
88 /** |
88 /** |
89 * Returns the number of elements in this set (its cardinality). If this |
89 * Returns the number of elements in this set (its cardinality). If this |
90 * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
90 * set contains more than {@code Integer.MAX_VALUE} elements, returns |
91 * <tt>Integer.MAX_VALUE</tt>. |
91 * {@code Integer.MAX_VALUE}. |
92 * |
92 * |
93 * @return the number of elements in this set (its cardinality) |
93 * @return the number of elements in this set (its cardinality) |
94 */ |
94 */ |
95 int size(); |
95 int size(); |
96 |
96 |
97 /** |
97 /** |
98 * Returns <tt>true</tt> if this set contains no elements. |
98 * Returns {@code true} if this set contains no elements. |
99 * |
99 * |
100 * @return <tt>true</tt> if this set contains no elements |
100 * @return {@code true} if this set contains no elements |
101 */ |
101 */ |
102 boolean isEmpty(); |
102 boolean isEmpty(); |
103 |
103 |
104 /** |
104 /** |
105 * Returns <tt>true</tt> if this set contains the specified element. |
105 * Returns {@code true} if this set contains the specified element. |
106 * More formally, returns <tt>true</tt> if and only if this set |
106 * More formally, returns {@code true} if and only if this set |
107 * contains an element <tt>e</tt> such that |
107 * contains an element {@code e} such that |
108 * <tt>(o==null ? e==null : o.equals(e))</tt>. |
108 * {@code Objects.equals(o, e)}. |
109 * |
109 * |
110 * @param o element whose presence in this set is to be tested |
110 * @param o element whose presence in this set is to be tested |
111 * @return <tt>true</tt> if this set contains the specified element |
111 * @return {@code true} if this set contains the specified element |
112 * @throws ClassCastException if the type of the specified element |
112 * @throws ClassCastException if the type of the specified element |
113 * is incompatible with this set |
113 * is incompatible with this set |
114 * (<a href="Collection.html#optional-restrictions">optional</a>) |
114 * (<a href="Collection.html#optional-restrictions">optional</a>) |
115 * @throws NullPointerException if the specified element is null and this |
115 * @throws NullPointerException if the specified element is null and this |
116 * set does not permit null elements |
116 * set does not permit null elements |
153 * specified array and the size of this set. |
153 * specified array and the size of this set. |
154 * |
154 * |
155 * <p>If this set fits in the specified array with room to spare |
155 * <p>If this set fits in the specified array with room to spare |
156 * (i.e., the array has more elements than this set), the element in |
156 * (i.e., the array has more elements than this set), the element in |
157 * the array immediately following the end of the set is set to |
157 * the array immediately following the end of the set is set to |
158 * <tt>null</tt>. (This is useful in determining the length of this |
158 * {@code null}. (This is useful in determining the length of this |
159 * set <i>only</i> if the caller knows that this set does not contain |
159 * set <i>only</i> if the caller knows that this set does not contain |
160 * any null elements.) |
160 * any null elements.) |
161 * |
161 * |
162 * <p>If this set makes any guarantees as to what order its elements |
162 * <p>If this set makes any guarantees as to what order its elements |
163 * are returned by its iterator, this method must return the elements |
163 * are returned by its iterator, this method must return the elements |
166 * <p>Like the {@link #toArray()} method, this method acts as bridge between |
166 * <p>Like the {@link #toArray()} method, this method acts as bridge between |
167 * array-based and collection-based APIs. Further, this method allows |
167 * array-based and collection-based APIs. Further, this method allows |
168 * precise control over the runtime type of the output array, and may, |
168 * precise control over the runtime type of the output array, and may, |
169 * under certain circumstances, be used to save allocation costs. |
169 * under certain circumstances, be used to save allocation costs. |
170 * |
170 * |
171 * <p>Suppose <tt>x</tt> is a set known to contain only strings. |
171 * <p>Suppose {@code x} is a set known to contain only strings. |
172 * The following code can be used to dump the set into a newly allocated |
172 * The following code can be used to dump the set into a newly allocated |
173 * array of <tt>String</tt>: |
173 * array of {@code String}: |
174 * |
174 * |
175 * <pre> |
175 * <pre> |
176 * String[] y = x.toArray(new String[0]);</pre> |
176 * String[] y = x.toArray(new String[0]);</pre> |
177 * |
177 * |
178 * Note that <tt>toArray(new Object[0])</tt> is identical in function to |
178 * Note that {@code toArray(new Object[0])} is identical in function to |
179 * <tt>toArray()</tt>. |
179 * {@code toArray()}. |
180 * |
180 * |
181 * @param a the array into which the elements of this set are to be |
181 * @param a the array into which the elements of this set are to be |
182 * stored, if it is big enough; otherwise, a new array of the same |
182 * stored, if it is big enough; otherwise, a new array of the same |
183 * runtime type is allocated for this purpose. |
183 * runtime type is allocated for this purpose. |
184 * @return an array containing all the elements in this set |
184 * @return an array containing all the elements in this set |
193 // Modification Operations |
193 // Modification Operations |
194 |
194 |
195 /** |
195 /** |
196 * Adds the specified element to this set if it is not already present |
196 * Adds the specified element to this set if it is not already present |
197 * (optional operation). More formally, adds the specified element |
197 * (optional operation). More formally, adds the specified element |
198 * <tt>e</tt> to this set if the set contains no element <tt>e2</tt> |
198 * {@code e} to this set if the set contains no element {@code e2} |
199 * such that |
199 * such that |
200 * <tt>(e==null ? e2==null : e.equals(e2))</tt>. |
200 * {@code Objects.equals(e, e2)}. |
201 * If this set already contains the element, the call leaves the set |
201 * If this set already contains the element, the call leaves the set |
202 * unchanged and returns <tt>false</tt>. In combination with the |
202 * unchanged and returns {@code false}. In combination with the |
203 * restriction on constructors, this ensures that sets never contain |
203 * restriction on constructors, this ensures that sets never contain |
204 * duplicate elements. |
204 * duplicate elements. |
205 * |
205 * |
206 * <p>The stipulation above does not imply that sets must accept all |
206 * <p>The stipulation above does not imply that sets must accept all |
207 * elements; sets may refuse to add any particular element, including |
207 * elements; sets may refuse to add any particular element, including |
208 * <tt>null</tt>, and throw an exception, as described in the |
208 * {@code null}, and throw an exception, as described in the |
209 * specification for {@link Collection#add Collection.add}. |
209 * specification for {@link Collection#add Collection.add}. |
210 * Individual set implementations should clearly document any |
210 * Individual set implementations should clearly document any |
211 * restrictions on the elements that they may contain. |
211 * restrictions on the elements that they may contain. |
212 * |
212 * |
213 * @param e element to be added to this set |
213 * @param e element to be added to this set |
214 * @return <tt>true</tt> if this set did not already contain the specified |
214 * @return {@code true} if this set did not already contain the specified |
215 * element |
215 * element |
216 * @throws UnsupportedOperationException if the <tt>add</tt> operation |
216 * @throws UnsupportedOperationException if the {@code add} operation |
217 * is not supported by this set |
217 * is not supported by this set |
218 * @throws ClassCastException if the class of the specified element |
218 * @throws ClassCastException if the class of the specified element |
219 * prevents it from being added to this set |
219 * prevents it from being added to this set |
220 * @throws NullPointerException if the specified element is null and this |
220 * @throws NullPointerException if the specified element is null and this |
221 * set does not permit null elements |
221 * set does not permit null elements |
225 boolean add(E e); |
225 boolean add(E e); |
226 |
226 |
227 |
227 |
228 /** |
228 /** |
229 * Removes the specified element from this set if it is present |
229 * Removes the specified element from this set if it is present |
230 * (optional operation). More formally, removes an element <tt>e</tt> |
230 * (optional operation). More formally, removes an element {@code e} |
231 * such that |
231 * such that |
232 * <tt>(o==null ? e==null : o.equals(e))</tt>, if |
232 * {@code Objects.equals(o, e)}, if |
233 * this set contains such an element. Returns <tt>true</tt> if this set |
233 * this set contains such an element. Returns {@code true} if this set |
234 * contained the element (or equivalently, if this set changed as a |
234 * contained the element (or equivalently, if this set changed as a |
235 * result of the call). (This set will not contain the element once the |
235 * result of the call). (This set will not contain the element once the |
236 * call returns.) |
236 * call returns.) |
237 * |
237 * |
238 * @param o object to be removed from this set, if present |
238 * @param o object to be removed from this set, if present |
239 * @return <tt>true</tt> if this set contained the specified element |
239 * @return {@code true} if this set contained the specified element |
240 * @throws ClassCastException if the type of the specified element |
240 * @throws ClassCastException if the type of the specified element |
241 * is incompatible with this set |
241 * is incompatible with this set |
242 * (<a href="Collection.html#optional-restrictions">optional</a>) |
242 * (<a href="Collection.html#optional-restrictions">optional</a>) |
243 * @throws NullPointerException if the specified element is null and this |
243 * @throws NullPointerException if the specified element is null and this |
244 * set does not permit null elements |
244 * set does not permit null elements |
245 * (<a href="Collection.html#optional-restrictions">optional</a>) |
245 * (<a href="Collection.html#optional-restrictions">optional</a>) |
246 * @throws UnsupportedOperationException if the <tt>remove</tt> operation |
246 * @throws UnsupportedOperationException if the {@code remove} operation |
247 * is not supported by this set |
247 * is not supported by this set |
248 */ |
248 */ |
249 boolean remove(Object o); |
249 boolean remove(Object o); |
250 |
250 |
251 |
251 |
252 // Bulk Operations |
252 // Bulk Operations |
253 |
253 |
254 /** |
254 /** |
255 * Returns <tt>true</tt> if this set contains all of the elements of the |
255 * Returns {@code true} if this set contains all of the elements of the |
256 * specified collection. If the specified collection is also a set, this |
256 * specified collection. If the specified collection is also a set, this |
257 * method returns <tt>true</tt> if it is a <i>subset</i> of this set. |
257 * method returns {@code true} if it is a <i>subset</i> of this set. |
258 * |
258 * |
259 * @param c collection to be checked for containment in this set |
259 * @param c collection to be checked for containment in this set |
260 * @return <tt>true</tt> if this set contains all of the elements of the |
260 * @return {@code true} if this set contains all of the elements of the |
261 * specified collection |
261 * specified collection |
262 * @throws ClassCastException if the types of one or more elements |
262 * @throws ClassCastException if the types of one or more elements |
263 * in the specified collection are incompatible with this |
263 * in the specified collection are incompatible with this |
264 * set |
264 * set |
265 * (<a href="Collection.html#optional-restrictions">optional</a>) |
265 * (<a href="Collection.html#optional-restrictions">optional</a>) |
273 boolean containsAll(Collection<?> c); |
273 boolean containsAll(Collection<?> c); |
274 |
274 |
275 /** |
275 /** |
276 * Adds all of the elements in the specified collection to this set if |
276 * Adds all of the elements in the specified collection to this set if |
277 * they're not already present (optional operation). If the specified |
277 * they're not already present (optional operation). If the specified |
278 * collection is also a set, the <tt>addAll</tt> operation effectively |
278 * collection is also a set, the {@code addAll} operation effectively |
279 * modifies this set so that its value is the <i>union</i> of the two |
279 * modifies this set so that its value is the <i>union</i> of the two |
280 * sets. The behavior of this operation is undefined if the specified |
280 * sets. The behavior of this operation is undefined if the specified |
281 * collection is modified while the operation is in progress. |
281 * collection is modified while the operation is in progress. |
282 * |
282 * |
283 * @param c collection containing elements to be added to this set |
283 * @param c collection containing elements to be added to this set |
284 * @return <tt>true</tt> if this set changed as a result of the call |
284 * @return {@code true} if this set changed as a result of the call |
285 * |
285 * |
286 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation |
286 * @throws UnsupportedOperationException if the {@code addAll} operation |
287 * is not supported by this set |
287 * is not supported by this set |
288 * @throws ClassCastException if the class of an element of the |
288 * @throws ClassCastException if the class of an element of the |
289 * specified collection prevents it from being added to this set |
289 * specified collection prevents it from being added to this set |
290 * @throws NullPointerException if the specified collection contains one |
290 * @throws NullPointerException if the specified collection contains one |
291 * or more null elements and this set does not permit null |
291 * or more null elements and this set does not permit null |
303 * specified collection. If the specified collection is also a set, this |
303 * specified collection. If the specified collection is also a set, this |
304 * operation effectively modifies this set so that its value is the |
304 * operation effectively modifies this set so that its value is the |
305 * <i>intersection</i> of the two sets. |
305 * <i>intersection</i> of the two sets. |
306 * |
306 * |
307 * @param c collection containing elements to be retained in this set |
307 * @param c collection containing elements to be retained in this set |
308 * @return <tt>true</tt> if this set changed as a result of the call |
308 * @return {@code true} if this set changed as a result of the call |
309 * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation |
309 * @throws UnsupportedOperationException if the {@code retainAll} operation |
310 * is not supported by this set |
310 * is not supported by this set |
311 * @throws ClassCastException if the class of an element of this set |
311 * @throws ClassCastException if the class of an element of this set |
312 * is incompatible with the specified collection |
312 * is incompatible with the specified collection |
313 * (<a href="Collection.html#optional-restrictions">optional</a>) |
313 * (<a href="Collection.html#optional-restrictions">optional</a>) |
314 * @throws NullPointerException if this set contains a null element and the |
314 * @throws NullPointerException if this set contains a null element and the |
325 * collection is also a set, this operation effectively modifies this |
325 * collection is also a set, this operation effectively modifies this |
326 * set so that its value is the <i>asymmetric set difference</i> of |
326 * set so that its value is the <i>asymmetric set difference</i> of |
327 * the two sets. |
327 * the two sets. |
328 * |
328 * |
329 * @param c collection containing elements to be removed from this set |
329 * @param c collection containing elements to be removed from this set |
330 * @return <tt>true</tt> if this set changed as a result of the call |
330 * @return {@code true} if this set changed as a result of the call |
331 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation |
331 * @throws UnsupportedOperationException if the {@code removeAll} operation |
332 * is not supported by this set |
332 * is not supported by this set |
333 * @throws ClassCastException if the class of an element of this set |
333 * @throws ClassCastException if the class of an element of this set |
334 * is incompatible with the specified collection |
334 * is incompatible with the specified collection |
335 * (<a href="Collection.html#optional-restrictions">optional</a>) |
335 * (<a href="Collection.html#optional-restrictions">optional</a>) |
336 * @throws NullPointerException if this set contains a null element and the |
336 * @throws NullPointerException if this set contains a null element and the |
344 |
344 |
345 /** |
345 /** |
346 * Removes all of the elements from this set (optional operation). |
346 * Removes all of the elements from this set (optional operation). |
347 * The set will be empty after this call returns. |
347 * The set will be empty after this call returns. |
348 * |
348 * |
349 * @throws UnsupportedOperationException if the <tt>clear</tt> method |
349 * @throws UnsupportedOperationException if the {@code clear} method |
350 * is not supported by this set |
350 * is not supported by this set |
351 */ |
351 */ |
352 void clear(); |
352 void clear(); |
353 |
353 |
354 |
354 |
355 // Comparison and hashing |
355 // Comparison and hashing |
356 |
356 |
357 /** |
357 /** |
358 * Compares the specified object with this set for equality. Returns |
358 * Compares the specified object with this set for equality. Returns |
359 * <tt>true</tt> if the specified object is also a set, the two sets |
359 * {@code true} if the specified object is also a set, the two sets |
360 * have the same size, and every member of the specified set is |
360 * have the same size, and every member of the specified set is |
361 * contained in this set (or equivalently, every member of this set is |
361 * contained in this set (or equivalently, every member of this set is |
362 * contained in the specified set). This definition ensures that the |
362 * contained in the specified set). This definition ensures that the |
363 * equals method works properly across different implementations of the |
363 * equals method works properly across different implementations of the |
364 * set interface. |
364 * set interface. |
365 * |
365 * |
366 * @param o object to be compared for equality with this set |
366 * @param o object to be compared for equality with this set |
367 * @return <tt>true</tt> if the specified object is equal to this set |
367 * @return {@code true} if the specified object is equal to this set |
368 */ |
368 */ |
369 boolean equals(Object o); |
369 boolean equals(Object o); |
370 |
370 |
371 /** |
371 /** |
372 * Returns the hash code value for this set. The hash code of a set is |
372 * Returns the hash code value for this set. The hash code of a set is |
373 * defined to be the sum of the hash codes of the elements in the set, |
373 * defined to be the sum of the hash codes of the elements in the set, |
374 * where the hash code of a <tt>null</tt> element is defined to be zero. |
374 * where the hash code of a {@code null} element is defined to be zero. |
375 * This ensures that <tt>s1.equals(s2)</tt> implies that |
375 * This ensures that {@code s1.equals(s2)} implies that |
376 * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt> |
376 * {@code s1.hashCode()==s2.hashCode()} for any two sets {@code s1} |
377 * and <tt>s2</tt>, as required by the general contract of |
377 * and {@code s2}, as required by the general contract of |
378 * {@link Object#hashCode}. |
378 * {@link Object#hashCode}. |
379 * |
379 * |
380 * @return the hash code value for this set |
380 * @return the hash code value for this set |
381 * @see Object#equals(Object) |
381 * @see Object#equals(Object) |
382 * @see Set#equals(Object) |
382 * @see Set#equals(Object) |