ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Collection.java
Revision: 1.3
Committed: Sun Oct 2 12:00:25 2005 UTC (18 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
Typo

File Contents

# User Rev Content
1 dl 1.1 /*
2 jsr166 1.2 * %W% %E%
3 dl 1.1 *
4     * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6     */
7    
8     package java.util;
9    
10     /**
11     * The root interface in the <i>collection hierarchy</i>. A collection
12     * represents a group of objects, known as its <i>elements</i>. Some
13     * collections allow duplicate elements and others do not. Some are ordered
14     * and others unordered. The JDK does not provide any <i>direct</i>
15     * implementations of this interface: it provides implementations of more
16     * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>. This interface
17     * is typically used to pass collections around and manipulate them where
18     * maximum generality is desired.
19     *
20     * <p><i>Bags</i> or <i>multisets</i> (unordered collections that may contain
21     * duplicate elements) should implement this interface directly.
22     *
23     * <p>All general-purpose <tt>Collection</tt> implementation classes (which
24     * typically implement <tt>Collection</tt> indirectly through one of its
25     * subinterfaces) should provide two "standard" constructors: a void (no
26     * arguments) constructor, which creates an empty collection, and a
27     * constructor with a single argument of type <tt>Collection</tt>, which
28     * creates a new collection with the same elements as its argument. In
29     * effect, the latter constructor allows the user to copy any collection,
30     * producing an equivalent collection of the desired implementation type.
31     * There is no way to enforce this convention (as interfaces cannot contain
32     * constructors) but all of the general-purpose <tt>Collection</tt>
33     * implementations in the Java platform libraries comply.
34     *
35     * <p>The "destructive" methods contained in this interface, that is, the
36     * methods that modify the collection on which they operate, are specified to
37     * throw <tt>UnsupportedOperationException</tt> if this collection does not
38     * support the operation. If this is the case, these methods may, but are not
39     * required to, throw an <tt>UnsupportedOperationException</tt> if the
40     * invocation would have no effect on the collection. For example, invoking
41     * the {@link #addAll(Collection)} method on an unmodifiable collection may,
42     * but is not required to, throw the exception if the collection to be added
43     * is empty.
44     *
45     * <p>Some collection implementations have restrictions on the elements that
46     * they may contain. For example, some implementations prohibit null elements,
47     * and some have restrictions on the types of their elements. Attempting to
48     * add an ineligible element throws an unchecked exception, typically
49     * <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. Attempting
50     * to query the presence of an ineligible element may throw an exception,
51     * or it may simply return false; some implementations will exhibit the former
52     * behavior and some will exhibit the latter. More generally, attempting an
53     * operation on an ineligible element whose completion would not result in
54     * the insertion of an ineligible element into the collection may throw an
55     * exception or it may succeed, at the option of the implementation.
56     * Such exceptions are marked as "optional" in the specification for this
57     * interface.
58     *
59     * <p>It is up to each collection to determine its own synchronization
60     * policy. In the absence of a stronger guarantee by the
61 dl 1.3 * implementation, undefined behavior may result from the invocation
62 dl 1.1 * of any method on a collection that is being mutated by another
63     * thread; this includes direct invocations, passing the collection to
64     * a method that might perform invocations, and using an existing
65     * iterator to examine the collection.
66     *
67     * <p>Many methods in Collections Framework interfaces are defined in
68     * terms of the {@link Object#equals(Object) equals} method. For example,
69     * the specification for the {@link #contains(Object) contains(Object o)}
70     * method says: "returns <tt>true</tt> if and only if this collection
71     * contains at least one element <tt>e</tt> such that
72     * <tt>(o==null ? e==null : o.equals(e))</tt>." This specification should
73     * <i>not</i> be construed to imply that invoking <tt>Collection.contains</tt>
74     * with a non-null argument <tt>o</tt> will cause <tt>o.equals(e)</tt> to be
75     * invoked for any element <tt>e</tt>. Implementations are free to implement
76     * optimizations whereby the <tt>equals</tt> invocation is avoided, for
77     * example, by first comparing the hash codes of the two elements. (The
78     * {@link Object#hashCode()} specification guarantees that two objects with
79     * unequal hash codes cannot be equal.) More generally, implementations of
80     * the various Collections Framework interfaces are free to take advantage of
81     * the specified behavior of underlying {@link Object} methods wherever the
82     * implementor deems it appropriate.
83     *
84     * <p>This interface is a member of the
85     * <a href="{@docRoot}/../guide/collections/index.html">
86     * Java Collections Framework</a>.
87     *
88     * @author Josh Bloch
89     * @author Neal Gafter
90 jsr166 1.2 * @version %I%, %G%
91 dl 1.1 * @see Set
92     * @see List
93     * @see Map
94     * @see SortedSet
95     * @see SortedMap
96     * @see HashSet
97     * @see TreeSet
98     * @see ArrayList
99     * @see LinkedList
100     * @see Vector
101     * @see Collections
102     * @see Arrays
103     * @see AbstractCollection
104     * @since 1.2
105     */
106    
107     public interface Collection<E> extends Iterable<E> {
108     // Query Operations
109    
110     /**
111     * Returns the number of elements in this collection. If this collection
112     * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
113     * <tt>Integer.MAX_VALUE</tt>.
114     *
115     * @return the number of elements in this collection
116     */
117     int size();
118    
119     /**
120     * Returns <tt>true</tt> if this collection contains no elements.
121     *
122     * @return <tt>true</tt> if this collection contains no elements
123     */
124     boolean isEmpty();
125    
126     /**
127     * Returns <tt>true</tt> if this collection contains the specified element.
128     * More formally, returns <tt>true</tt> if and only if this collection
129     * contains at least one element <tt>e</tt> such that
130     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
131     *
132     * @param o element whose presence in this collection is to be tested
133     * @return <tt>true</tt> if this collection contains the specified
134     * element
135     * @throws ClassCastException if the type of the specified element
136     * is incompatible with this collection (optional)
137     * @throws NullPointerException if the specified element is null and this
138     * collection does not permit null elements (optional)
139     */
140     boolean contains(Object o);
141    
142     /**
143     * Returns an iterator over the elements in this collection. There are no
144     * guarantees concerning the order in which the elements are returned
145     * (unless this collection is an instance of some class that provides a
146     * guarantee).
147     *
148     * @return an <tt>Iterator</tt> over the elements in this collection
149     */
150     Iterator<E> iterator();
151    
152     /**
153     * Returns an array containing all of the elements in this collection.
154     * If this collection makes any guarantees as to what order its elements
155     * are returned by its iterator, this method must return the elements in
156     * the same order.
157     *
158     * <p>The returned array will be "safe" in that no references to it are
159     * maintained by this collection. (In other words, this method must
160     * allocate a new array even if this collection is backed by an array).
161     * The caller is thus free to modify the returned array.
162     *
163     * <p>This method acts as bridge between array-based and collection-based
164     * APIs.
165     *
166     * @return an array containing all of the elements in this collection
167     */
168     Object[] toArray();
169    
170     /**
171     * Returns an array containing all of the elements in this collection;
172     * the runtime type of the returned array is that of the specified array.
173     * If the collection fits in the specified array, it is returned therein.
174     * Otherwise, a new array is allocated with the runtime type of the
175     * specified array and the size of this collection.
176     *
177     * <p>If this collection fits in the specified array with room to spare
178     * (i.e., the array has more elements than this collection), the element
179     * in the array immediately following the end of the collection is set to
180     * <tt>null</tt>. (This is useful in determining the length of this
181     * collection <i>only</i> if the caller knows that this collection does
182     * not contain any <tt>null</tt> elements.)
183     *
184     * <p>If this collection makes any guarantees as to what order its elements
185     * are returned by its iterator, this method must return the elements in
186     * the same order.
187     *
188     * <p>Like the {@link #toArray()} method, this method acts as bridge between
189     * array-based and collection-based APIs. Further, this method allows
190     * precise control over the runtime type of the output array, and may,
191     * under certain circumstances, be used to save allocation costs.
192     *
193     * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
194     * The following code can be used to dump the collection into a newly
195     * allocated array of <tt>String</tt>:
196     *
197     * <pre>
198     * String[] y = x.toArray(new String[0]);</pre>
199     *
200     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
201     * <tt>toArray()</tt>.
202     *
203     * @param a the array into which the elements of this collection are to be
204     * stored, if it is big enough; otherwise, a new array of the same
205     * runtime type is allocated for this purpose.
206     * @return an array containing all of the elements in this collection
207     * @throws ArrayStoreException if the runtime type of the specified array
208     * is not a supertype of the runtime type of every element in
209     * this collection
210     * @throws NullPointerException if the specified array is null
211     */
212     <T> T[] toArray(T[] a);
213    
214     // Modification Operations
215    
216     /**
217     * Ensures that this collection contains the specified element (optional
218     * operation). Returns <tt>true</tt> if this collection changed as a
219     * result of the call. (Returns <tt>false</tt> if this collection does
220     * not permit duplicates and already contains the specified element.)<p>
221     *
222     * Collections that support this operation may place limitations on what
223     * elements may be added to this collection. In particular, some
224     * collections will refuse to add <tt>null</tt> elements, and others will
225     * impose restrictions on the type of elements that may be added.
226     * Collection classes should clearly specify in their documentation any
227     * restrictions on what elements may be added.<p>
228     *
229     * If a collection refuses to add a particular element for any reason
230     * other than that it already contains the element, it <i>must</i> throw
231     * an exception (rather than returning <tt>false</tt>). This preserves
232     * the invariant that a collection always contains the specified element
233     * after this call returns.
234     *
235     * @param e element whose presence in this collection is to be ensured
236     * @return <tt>true</tt> if this collection changed as a result of the
237     * call
238     * @throws UnsupportedOperationException if the <tt>add</tt> operation
239     * is not supported by this collection
240     * @throws ClassCastException if the class of the specified element
241     * prevents it from being added to this collection
242     * @throws NullPointerException if the specified element is null and this
243     * collection does not permit null elements
244     * @throws IllegalArgumentException if some property of the element
245     * prevents it from being added to this collection
246     * @throws IllegalStateException if the element cannot be added at this
247     * time due to insertion restrictions
248     */
249     boolean add(E e);
250    
251     /**
252     * Removes a single instance of the specified element from this
253     * collection, if it is present (optional operation). More formally,
254     * removes an element <tt>e</tt> such that
255     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
256     * this collection contains one or more such elements. Returns
257     * <tt>true</tt> if this collection contained the specified element (or
258     * equivalently, if this collection changed as a result of the call).
259     *
260     * @param o element to be removed from this collection, if present
261     * @return <tt>true</tt> if an element was removed as a result of this call
262     * @throws ClassCastException if the type of the specified element
263     * is incompatible with this collection (optional)
264     * @throws NullPointerException if the specified element is null and this
265     * collection does not permit null elements (optional)
266     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
267     * is not supported by this collection
268     */
269     boolean remove(Object o);
270    
271    
272     // Bulk Operations
273    
274     /**
275     * Returns <tt>true</tt> if this collection contains all of the elements
276     * in the specified collection.
277     *
278     * @param c collection to be checked for containment in this collection
279     * @return <tt>true</tt> if this collection contains all of the elements
280     * in the specified collection
281     * @throws ClassCastException if the types of one or more elements
282     * in the specified collection are incompatible with this
283     * collection (optional)
284     * @throws NullPointerException if the specified collection contains one
285     * or more null elements and this collection does not permit null
286     * elements (optional), or if the specified collection is null
287     * @see #contains(Object)
288     */
289     boolean containsAll(Collection<?> c);
290    
291     /**
292     * Adds all of the elements in the specified collection to this collection
293     * (optional operation). The behavior of this operation is undefined if
294     * the specified collection is modified while the operation is in progress.
295     * (This implies that the behavior of this call is undefined if the
296     * specified collection is this collection, and this collection is
297     * nonempty.)
298     *
299     * @param c collection containing elements to be added to this collection
300     * @return <tt>true</tt> if this collection changed as a result of the call
301     * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
302     * is not supported by this collection
303     * @throws ClassCastException if the class of an element of the specified
304     * collection prevents it from being added to this collection
305     * @throws NullPointerException if the specified collection contains a
306     * null element and this collection does not permit null elements,
307     * or if the specified collection is null
308     * @throws IllegalArgumentException if some property of an element of the
309     * specified collection prevents it from being added to this
310     * collection
311     * @throws IllegalStateException if not all the elements can be added at
312     * this time due to insertion restrictions
313     * @see #add(Object)
314     */
315     boolean addAll(Collection<? extends E> c);
316    
317     /**
318     * Removes all of this collection's elements that are also contained in the
319     * specified collection (optional operation). After this call returns,
320     * this collection will contain no elements in common with the specified
321     * collection.
322     *
323     * @param c collection containing elements to be removed from this collection
324     * @return <tt>true</tt> if this collection changed as a result of the
325     * call
326     * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
327     * is not supported by this collection
328     * @throws ClassCastException if the types of one or more elements
329     * in this collection are incompatible with the specified
330     * collection (optional)
331     * @throws NullPointerException if this collection contains one or more
332     * null elements and the specified collection does not support
333     * null elements (optional), or if the specified collection is null
334     * @see #remove(Object)
335     * @see #contains(Object)
336     */
337     boolean removeAll(Collection<?> c);
338    
339     /**
340     * Retains only the elements in this collection that are contained in the
341     * specified collection (optional operation). In other words, removes from
342     * this collection all of its elements that are not contained in the
343     * specified collection.
344     *
345     * @param c collection containing elements to be retained in this collection
346     * @return <tt>true</tt> if this collection changed as a result of the call
347     * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
348     * is not supported by this collection
349     * @throws ClassCastException if the types of one or more elements
350     * in this collection are incompatible with the specified
351     * collection (optional)
352     * @throws NullPointerException if this collection contains one or more
353     * null elements and the specified collection does not permit null
354     * elements (optional), or if the specified collection is null
355     * @see #remove(Object)
356     * @see #contains(Object)
357     */
358     boolean retainAll(Collection<?> c);
359    
360     /**
361     * Removes all of the elements from this collection (optional operation).
362     * The collection will be empty after this method returns.
363     *
364     * @throws UnsupportedOperationException if the <tt>clear</tt> operation
365     * is not supported by this collection
366     */
367     void clear();
368    
369    
370     // Comparison and hashing
371    
372     /**
373     * Compares the specified object with this collection for equality. <p>
374     *
375     * While the <tt>Collection</tt> interface adds no stipulations to the
376     * general contract for the <tt>Object.equals</tt>, programmers who
377     * implement the <tt>Collection</tt> interface "directly" (in other words,
378     * create a class that is a <tt>Collection</tt> but is not a <tt>Set</tt>
379     * or a <tt>List</tt>) must exercise care if they choose to override the
380     * <tt>Object.equals</tt>. It is not necessary to do so, and the simplest
381     * course of action is to rely on <tt>Object</tt>'s implementation, but
382     * the implementor may wish to implement a "value comparison" in place of
383     * the default "reference comparison." (The <tt>List</tt> and
384     * <tt>Set</tt> interfaces mandate such value comparisons.)<p>
385     *
386     * The general contract for the <tt>Object.equals</tt> method states that
387     * equals must be symmetric (in other words, <tt>a.equals(b)</tt> if and
388     * only if <tt>b.equals(a)</tt>). The contracts for <tt>List.equals</tt>
389     * and <tt>Set.equals</tt> state that lists are only equal to other lists,
390     * and sets to other sets. Thus, a custom <tt>equals</tt> method for a
391     * collection class that implements neither the <tt>List</tt> nor
392     * <tt>Set</tt> interface must return <tt>false</tt> when this collection
393     * is compared to any list or set. (By the same logic, it is not possible
394     * to write a class that correctly implements both the <tt>Set</tt> and
395     * <tt>List</tt> interfaces.)
396     *
397     * @param o object to be compared for equality with this collection
398     * @return <tt>true</tt> if the specified object is equal to this
399     * collection
400     *
401     * @see Object#equals(Object)
402     * @see Set#equals(Object)
403     * @see List#equals(Object)
404     */
405     boolean equals(Object o);
406    
407     /**
408     * Returns the hash code value for this collection. While the
409     * <tt>Collection</tt> interface adds no stipulations to the general
410     * contract for the <tt>Object.hashCode</tt> method, programmers should
411     * take note that any class that overrides the <tt>Object.equals</tt>
412     * method must also override the <tt>Object.hashCode</tt> method in order
413     * to satisfy the general contract for the <tt>Object.hashCode</tt>method.
414     * In particular, <tt>c1.equals(c2)</tt> implies that
415     * <tt>c1.hashCode()==c2.hashCode()</tt>.
416     *
417     * @return the hash code value for this collection
418     *
419     * @see Object#hashCode()
420     * @see Object#equals(Object)
421     */
422     int hashCode();
423     }