001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util;
027
028 /**
029 * A collection that contains no duplicate elements. More formally, sets
030 * contain no pair of elements <code>e1</code> and <code>e2</code> such that
031 * <code>e1.equals(e2)</code>, and at most one null element. As implied by
032 * its name, this interface models the mathematical <i>set</i> abstraction.
033 *
034 * <p>The <tt>Set</tt> interface places additional stipulations, beyond those
035 * inherited from the <tt>Collection</tt> interface, on the contracts of all
036 * constructors and on the contracts of the <tt>add</tt>, <tt>equals</tt> and
037 * <tt>hashCode</tt> methods. Declarations for other inherited methods are
038 * also included here for convenience. (The specifications accompanying these
039 * declarations have been tailored to the <tt>Set</tt> interface, but they do
040 * not contain any additional stipulations.)
041 *
042 * <p>The additional stipulation on constructors is, not surprisingly,
043 * that all constructors must create a set that contains no duplicate elements
044 * (as defined above).
045 *
046 * <p>Note: Great care must be exercised if mutable objects are used as set
047 * elements. The behavior of a set is not specified if the value of an object
048 * is changed in a manner that affects <tt>equals</tt> comparisons while the
049 * object is an element in the set. A special case of this prohibition is
050 * that it is not permissible for a set to contain itself as an element.
051 *
052 * <p>Some set implementations have restrictions on the elements that
053 * they may contain. For example, some implementations prohibit null elements,
054 * and some have restrictions on the types of their elements. Attempting to
055 * add an ineligible element throws an unchecked exception, typically
056 * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
057 * to query the presence of an ineligible element may throw an exception,
058 * or it may simply return false; some implementations will exhibit the former
059 * behavior and some will exhibit the latter. More generally, attempting an
060 * operation on an ineligible element whose completion would not result in
061 * the insertion of an ineligible element into the set may throw an
062 * exception or it may succeed, at the option of the implementation.
063 * Such exceptions are marked as "optional" in the specification for this
064 * interface.
065 *
066 * <p>This interface is a member of the
067 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
068 * Java Collections Framework</a>.
069 *
070 * @param <E> the type of elements maintained by this set
071 *
072 * @author Josh Bloch
073 * @author Neal Gafter
074 * @version 1.45, 05/05/07
075 * @see Collection
076 * @see List
077 * @see SortedSet
078 * @see HashSet
079 * @see TreeSet
080 * @see AbstractSet
081 * @see Collections#singleton(java.lang.Object)
082 * @see Collections#EMPTY_SET
083 * @since 1.2
084 */
085
086 public interface Set<E> extends Collection<E> {
087 // Query Operations
088
089 /**
090 * Returns the number of elements in this set (its cardinality). If this
091 * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
092 * <tt>Integer.MAX_VALUE</tt>.
093 *
094 * @return the number of elements in this set (its cardinality)
095 */
096 int size();
097
098 /**
099 * Returns <tt>true</tt> if this set contains no elements.
100 *
101 * @return <tt>true</tt> if this set contains no elements
102 */
103 boolean isEmpty();
104
105 /**
106 * Returns <tt>true</tt> if this set contains the specified element.
107 * More formally, returns <tt>true</tt> if and only if this set
108 * contains an element <tt>e</tt> such that
109 * <tt>(o==null ? e==null : o.equals(e))</tt>.
110 *
111 * @param o element whose presence in this set is to be tested
112 * @return <tt>true</tt> if this set contains the specified element
113 * @throws ClassCastException if the type of the specified element
114 * is incompatible with this set (optional)
115 * @throws NullPointerException if the specified element is null and this
116 * set does not permit null elements (optional)
117 */
118 boolean contains(Object o);
119
120 /**
121 * Returns an iterator over the elements in this set. The elements are
122 * returned in no particular order (unless this set is an instance of some
123 * class that provides a guarantee).
124 *
125 * @return an iterator over the elements in this set
126 */
127 Iterator<E> iterator();
128
129 /**
130 * Returns an array containing all of the elements in this set.
131 * If this set makes any guarantees as to what order its elements
132 * are returned by its iterator, this method must return the
133 * elements in the same order.
134 *
135 * <p>The returned array will be "safe" in that no references to it
136 * are maintained by this set. (In other words, this method must
137 * allocate a new array even if this set is backed by an array).
138 * The caller is thus free to modify the returned array.
139 *
140 * <p>This method acts as bridge between array-based and collection-based
141 * APIs.
142 *
143 * @return an array containing all the elements in this set
144 */
145 Object[] toArray();
146
147 /**
148 * Returns an array containing all of the elements in this set; the
149 * runtime type of the returned array is that of the specified array.
150 * If the set fits in the specified array, it is returned therein.
151 * Otherwise, a new array is allocated with the runtime type of the
152 * specified array and the size of this set.
153 *
154 * <p>If this set fits in the specified array with room to spare
155 * (i.e., the array has more elements than this set), the element in
156 * the array immediately following the end of the set is set to
157 * <tt>null</tt>. (This is useful in determining the length of this
158 * set <i>only</i> if the caller knows that this set does not contain
159 * any null elements.)
160 *
161 * <p>If this set makes any guarantees as to what order its elements
162 * are returned by its iterator, this method must return the elements
163 * in the same order.
164 *
165 * <p>Like the {@link #toArray()} method, this method acts as bridge between
166 * array-based and collection-based APIs. Further, this method allows
167 * precise control over the runtime type of the output array, and may,
168 * under certain circumstances, be used to save allocation costs.
169 *
170 * <p>Suppose <tt>x</tt> is a set known to contain only strings.
171 * The following code can be used to dump the set into a newly allocated
172 * array of <tt>String</tt>:
173 *
174 * <pre>
175 * String[] y = x.toArray(new String[0]);</pre>
176 *
177 * Note that <tt>toArray(new Object[0])</tt> is identical in function to
178 * <tt>toArray()</tt>.
179 *
180 * @param a the array into which the elements of this set are to be
181 * stored, if it is big enough; otherwise, a new array of the same
182 * runtime type is allocated for this purpose.
183 * @return an array containing all the elements in this set
184 * @throws ArrayStoreException if the runtime type of the specified array
185 * is not a supertype of the runtime type of every element in this
186 * set
187 * @throws NullPointerException if the specified array is null
188 */
189 <T> T[] toArray(T[] a);
190
191 // Modification Operations
192
193 /**
194 * Adds the specified element to this set if it is not already present
195 * (optional operation). More formally, adds the specified element
196 * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
197 * such that
198 * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
199 * If this set already contains the element, the call leaves the set
200 * unchanged and returns <tt>false</tt>. In combination with the
201 * restriction on constructors, this ensures that sets never contain
202 * duplicate elements.
203 *
204 * <p>The stipulation above does not imply that sets must accept all
205 * elements; sets may refuse to add any particular element, including
206 * <tt>null</tt>, and throw an exception, as described in the
207 * specification for {@link Collection#add Collection.add}.
208 * Individual set implementations should clearly document any
209 * restrictions on the elements that they may contain.
210 *
211 * @param e element to be added to this set
212 * @return <tt>true</tt> if this set did not already contain the specified
213 * element
214 * @throws UnsupportedOperationException if the <tt>add</tt> operation
215 * is not supported by this set
216 * @throws ClassCastException if the class of the specified element
217 * prevents it from being added to this set
218 * @throws NullPointerException if the specified element is null and this
219 * set does not permit null elements
220 * @throws IllegalArgumentException if some property of the specified element
221 * prevents it from being added to this set
222 */
223 boolean add(E e);
224
225 /**
226 * Removes the specified element from this set if it is present
227 * (optional operation). More formally, removes an element <tt>e</tt>
228 * such that
229 * <tt>(o==null ? e==null : o.equals(e))</tt>, if
230 * this set contains such an element. Returns <tt>true</tt> if this set
231 * contained the element (or equivalently, if this set changed as a
232 * result of the call). (This set will not contain the element once the
233 * call returns.)
234 *
235 * @param o object to be removed from this set, if present
236 * @return <tt>true</tt> if this set contained the specified element
237 * @throws ClassCastException if the type of the specified element
238 * is incompatible with this set (optional)
239 * @throws NullPointerException if the specified element is null and this
240 * set does not permit null elements (optional)
241 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
242 * is not supported by this set
243 */
244 boolean remove(Object o);
245
246 // Bulk Operations
247
248 /**
249 * Returns <tt>true</tt> if this set contains all of the elements of the
250 * specified collection. If the specified collection is also a set, this
251 * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
252 *
253 * @param c collection to be checked for containment in this set
254 * @return <tt>true</tt> if this set contains all of the elements of the
255 * specified collection
256 * @throws ClassCastException if the types of one or more elements
257 * in the specified collection are incompatible with this
258 * set (optional)
259 * @throws NullPointerException if the specified collection contains one
260 * or more null elements and this set does not permit null
261 * elements (optional), or if the specified collection is null
262 * @see #contains(Object)
263 */
264 boolean containsAll(Collection<?> c);
265
266 /**
267 * Adds all of the elements in the specified collection to this set if
268 * they're not already present (optional operation). If the specified
269 * collection is also a set, the <tt>addAll</tt> operation effectively
270 * modifies this set so that its value is the <i>union</i> of the two
271 * sets. The behavior of this operation is undefined if the specified
272 * collection is modified while the operation is in progress.
273 *
274 * @param c collection containing elements to be added to this set
275 * @return <tt>true</tt> if this set changed as a result of the call
276 *
277 * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
278 * is not supported by this set
279 * @throws ClassCastException if the class of an element of the
280 * specified collection prevents it from being added to this set
281 * @throws NullPointerException if the specified collection contains one
282 * or more null elements and this set does not permit null
283 * elements, or if the specified collection is null
284 * @throws IllegalArgumentException if some property of an element of the
285 * specified collection prevents it from being added to this set
286 * @see #add(Object)
287 */
288 boolean addAll(Collection<? extends E> c);
289
290 /**
291 * Retains only the elements in this set that are contained in the
292 * specified collection (optional operation). In other words, removes
293 * from this set all of its elements that are not contained in the
294 * specified collection. If the specified collection is also a set, this
295 * operation effectively modifies this set so that its value is the
296 * <i>intersection</i> of the two sets.
297 *
298 * @param c collection containing elements to be retained in this set
299 * @return <tt>true</tt> if this set changed as a result of the call
300 * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
301 * is not supported by this set
302 * @throws ClassCastException if the class of an element of this set
303 * is incompatible with the specified collection (optional)
304 * @throws NullPointerException if this set contains a null element and the
305 * specified collection does not permit null elements (optional),
306 * or if the specified collection is null
307 * @see #remove(Object)
308 */
309 boolean retainAll(Collection<?> c);
310
311 /**
312 * Removes from this set all of its elements that are contained in the
313 * specified collection (optional operation). If the specified
314 * collection is also a set, this operation effectively modifies this
315 * set so that its value is the <i>asymmetric set difference</i> of
316 * the two sets.
317 *
318 * @param c collection containing elements to be removed from this set
319 * @return <tt>true</tt> if this set changed as a result of the call
320 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
321 * is not supported by this set
322 * @throws ClassCastException if the class of an element of this set
323 * is incompatible with the specified collection (optional)
324 * @throws NullPointerException if this set contains a null element and the
325 * specified collection does not permit null elements (optional),
326 * or if the specified collection is null
327 * @see #remove(Object)
328 * @see #contains(Object)
329 */
330 boolean removeAll(Collection<?> c);
331
332 /**
333 * Removes all of the elements from this set (optional operation).
334 * The set will be empty after this call returns.
335 *
336 * @throws UnsupportedOperationException if the <tt>clear</tt> method
337 * is not supported by this set
338 */
339 void clear();
340
341 // Comparison and hashing
342
343 /**
344 * Compares the specified object with this set for equality. Returns
345 * <tt>true</tt> if the specified object is also a set, the two sets
346 * have the same size, and every member of the specified set is
347 * contained in this set (or equivalently, every member of this set is
348 * contained in the specified set). This definition ensures that the
349 * equals method works properly across different implementations of the
350 * set interface.
351 *
352 * @param o object to be compared for equality with this set
353 * @return <tt>true</tt> if the specified object is equal to this set
354 */
355 boolean equals(Object o);
356
357 /**
358 * Returns the hash code value for this set. The hash code of a set is
359 * defined to be the sum of the hash codes of the elements in the set,
360 * where the hash code of a <tt>null</tt> element is defined to be zero.
361 * This ensures that <tt>s1.equals(s2)</tt> implies that
362 * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
363 * and <tt>s2</tt>, as required by the general contract of
364 * {@link Object#hashCode}.
365 *
366 * @return the hash code value for this set
367 * @see Object#equals(Object)
368 * @see Set#equals(Object)
369 */
370 int hashCode();
371 }
|