jaroslav@557: /* jaroslav@557: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@557: * jaroslav@557: * This code is free software; you can redistribute it and/or modify it jaroslav@557: * under the terms of the GNU General Public License version 2 only, as jaroslav@557: * published by the Free Software Foundation. Oracle designates this jaroslav@557: * particular file as subject to the "Classpath" exception as provided jaroslav@557: * by Oracle in the LICENSE file that accompanied this code. jaroslav@557: * jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@557: * accompanied this code). jaroslav@557: * jaroslav@557: * You should have received a copy of the GNU General Public License version jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@557: * jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@557: * or visit www.oracle.com if you need additional information or have any jaroslav@557: * questions. jaroslav@557: */ jaroslav@557: jaroslav@557: package java.util; jaroslav@557: jaroslav@557: /** jaroslav@557: * The root interface in the collection hierarchy. A collection jaroslav@557: * represents a group of objects, known as its elements. Some jaroslav@557: * collections allow duplicate elements and others do not. Some are ordered jaroslav@557: * and others unordered. The JDK does not provide any direct jaroslav@557: * implementations of this interface: it provides implementations of more jaroslav@557: * specific subinterfaces like Set and List. This interface jaroslav@557: * is typically used to pass collections around and manipulate them where jaroslav@557: * maximum generality is desired. jaroslav@557: * jaroslav@557: *
Bags or multisets (unordered collections that may contain jaroslav@557: * duplicate elements) should implement this interface directly. jaroslav@557: * jaroslav@557: *
All general-purpose Collection implementation classes (which jaroslav@557: * typically implement Collection indirectly through one of its jaroslav@557: * subinterfaces) should provide two "standard" constructors: a void (no jaroslav@557: * arguments) constructor, which creates an empty collection, and a jaroslav@557: * constructor with a single argument of type Collection, which jaroslav@557: * creates a new collection with the same elements as its argument. In jaroslav@557: * effect, the latter constructor allows the user to copy any collection, jaroslav@557: * producing an equivalent collection of the desired implementation type. jaroslav@557: * There is no way to enforce this convention (as interfaces cannot contain jaroslav@557: * constructors) but all of the general-purpose Collection jaroslav@557: * implementations in the Java platform libraries comply. jaroslav@557: * jaroslav@557: *
The "destructive" methods contained in this interface, that is, the jaroslav@557: * methods that modify the collection on which they operate, are specified to jaroslav@557: * throw UnsupportedOperationException if this collection does not jaroslav@557: * support the operation. If this is the case, these methods may, but are not jaroslav@557: * required to, throw an UnsupportedOperationException if the jaroslav@557: * invocation would have no effect on the collection. For example, invoking jaroslav@557: * the {@link #addAll(Collection)} method on an unmodifiable collection may, jaroslav@557: * but is not required to, throw the exception if the collection to be added jaroslav@557: * is empty. jaroslav@557: * jaroslav@557: *
jaroslav@557: * Some collection implementations have restrictions on the elements that jaroslav@557: * they may contain. For example, some implementations prohibit null elements, jaroslav@557: * and some have restrictions on the types of their elements. Attempting to jaroslav@557: * add an ineligible element throws an unchecked exception, typically jaroslav@557: * NullPointerException or ClassCastException. Attempting jaroslav@557: * to query the presence of an ineligible element may throw an exception, jaroslav@557: * or it may simply return false; some implementations will exhibit the former jaroslav@557: * behavior and some will exhibit the latter. More generally, attempting an jaroslav@557: * operation on an ineligible element whose completion would not result in jaroslav@557: * the insertion of an ineligible element into the collection may throw an jaroslav@557: * exception or it may succeed, at the option of the implementation. jaroslav@557: * Such exceptions are marked as "optional" in the specification for this jaroslav@557: * interface. jaroslav@557: * jaroslav@557: *
It is up to each collection to determine its own synchronization jaroslav@557: * policy. In the absence of a stronger guarantee by the jaroslav@557: * implementation, undefined behavior may result from the invocation jaroslav@557: * of any method on a collection that is being mutated by another jaroslav@557: * thread; this includes direct invocations, passing the collection to jaroslav@557: * a method that might perform invocations, and using an existing jaroslav@557: * iterator to examine the collection. jaroslav@557: * jaroslav@557: *
Many methods in Collections Framework interfaces are defined in jaroslav@557: * terms of the {@link Object#equals(Object) equals} method. For example, jaroslav@557: * the specification for the {@link #contains(Object) contains(Object o)} jaroslav@557: * method says: "returns true if and only if this collection jaroslav@557: * contains at least one element e such that jaroslav@557: * (o==null ? e==null : o.equals(e))." This specification should jaroslav@557: * not be construed to imply that invoking Collection.contains jaroslav@557: * with a non-null argument o will cause o.equals(e) to be jaroslav@557: * invoked for any element e. Implementations are free to implement jaroslav@557: * optimizations whereby the equals invocation is avoided, for jaroslav@557: * example, by first comparing the hash codes of the two elements. (The jaroslav@557: * {@link Object#hashCode()} specification guarantees that two objects with jaroslav@557: * unequal hash codes cannot be equal.) More generally, implementations of jaroslav@557: * the various Collections Framework interfaces are free to take advantage of jaroslav@557: * the specified behavior of underlying {@link Object} methods wherever the jaroslav@557: * implementor deems it appropriate. jaroslav@557: * jaroslav@557: *
This interface is a member of the
jaroslav@557: *
jaroslav@557: * Java Collections Framework.
jaroslav@557: *
jaroslav@557: * @param The returned array will be "safe" in that no references to it are
jaroslav@557: * maintained by this collection. (In other words, this method must
jaroslav@557: * allocate a new array even if this collection is backed by an array).
jaroslav@557: * The caller is thus free to modify the returned array.
jaroslav@557: *
jaroslav@557: * This method acts as bridge between array-based and collection-based
jaroslav@557: * APIs.
jaroslav@557: *
jaroslav@557: * @return an array containing all of the elements in this collection
jaroslav@557: */
jaroslav@557: Object[] toArray();
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns an array containing all of the elements in this collection;
jaroslav@557: * the runtime type of the returned array is that of the specified array.
jaroslav@557: * If the collection fits in the specified array, it is returned therein.
jaroslav@557: * Otherwise, a new array is allocated with the runtime type of the
jaroslav@557: * specified array and the size of this collection.
jaroslav@557: *
jaroslav@557: * If this collection fits in the specified array with room to spare
jaroslav@557: * (i.e., the array has more elements than this collection), the element
jaroslav@557: * in the array immediately following the end of the collection is set to
jaroslav@557: * null. (This is useful in determining the length of this
jaroslav@557: * collection only if the caller knows that this collection does
jaroslav@557: * not contain any null elements.)
jaroslav@557: *
jaroslav@557: * If this collection makes any guarantees as to what order its elements
jaroslav@557: * are returned by its iterator, this method must return the elements in
jaroslav@557: * the same order.
jaroslav@557: *
jaroslav@557: * Like the {@link #toArray()} method, this method acts as bridge between
jaroslav@557: * array-based and collection-based APIs. Further, this method allows
jaroslav@557: * precise control over the runtime type of the output array, and may,
jaroslav@557: * under certain circumstances, be used to save allocation costs.
jaroslav@557: *
jaroslav@557: * Suppose x is a collection known to contain only strings.
jaroslav@557: * The following code can be used to dump the collection into a newly
jaroslav@557: * allocated array of String:
jaroslav@557: *
jaroslav@557: *
jaroslav@557: *
jaroslav@557: * Collections that support this operation may place limitations on what
jaroslav@557: * elements may be added to this collection. In particular, some
jaroslav@557: * collections will refuse to add null elements, and others will
jaroslav@557: * impose restrictions on the type of elements that may be added.
jaroslav@557: * Collection classes should clearly specify in their documentation any
jaroslav@557: * restrictions on what elements may be added.
jaroslav@557: *
jaroslav@557: * If a collection refuses to add a particular element for any reason
jaroslav@557: * other than that it already contains the element, it must throw
jaroslav@557: * an exception (rather than returning false). This preserves
jaroslav@557: * the invariant that a collection always contains the specified element
jaroslav@557: * after this call returns.
jaroslav@557: *
jaroslav@557: * @param e element whose presence in this collection is to be ensured
jaroslav@557: * @return true if this collection changed as a result of the
jaroslav@557: * call
jaroslav@557: * @throws UnsupportedOperationException if the add operation
jaroslav@557: * is not supported by this collection
jaroslav@557: * @throws ClassCastException if the class of the specified element
jaroslav@557: * prevents it from being added to this collection
jaroslav@557: * @throws NullPointerException if the specified element is null and this
jaroslav@557: * collection does not permit null elements
jaroslav@557: * @throws IllegalArgumentException if some property of the element
jaroslav@557: * prevents it from being added to this collection
jaroslav@557: * @throws IllegalStateException if the element cannot be added at this
jaroslav@557: * time due to insertion restrictions
jaroslav@557: */
jaroslav@557: boolean add(E e);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes a single instance of the specified element from this
jaroslav@557: * collection, if it is present (optional operation). More formally,
jaroslav@557: * removes an element e such that
jaroslav@557: * (o==null ? e==null : o.equals(e)), if
jaroslav@557: * this collection contains one or more such elements. Returns
jaroslav@557: * true if this collection contained the specified element (or
jaroslav@557: * equivalently, if this collection changed as a result of the call).
jaroslav@557: *
jaroslav@557: * @param o element to be removed from this collection, if present
jaroslav@557: * @return true if an element was removed as a result of this call
jaroslav@557: * @throws ClassCastException if the type of the specified element
jaroslav@557: * is incompatible with this collection
jaroslav@557: * (optional)
jaroslav@557: * @throws NullPointerException if the specified element is null and this
jaroslav@557: * collection does not permit null elements
jaroslav@557: * (optional)
jaroslav@557: * @throws UnsupportedOperationException if the remove operation
jaroslav@557: * is not supported by this collection
jaroslav@557: */
jaroslav@557: boolean remove(Object o);
jaroslav@557:
jaroslav@557:
jaroslav@557: // Bulk Operations
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns true if this collection contains all of the elements
jaroslav@557: * in the specified collection.
jaroslav@557: *
jaroslav@557: * @param c collection to be checked for containment in this collection
jaroslav@557: * @return true if this collection contains all of the elements
jaroslav@557: * in the specified collection
jaroslav@557: * @throws ClassCastException if the types of one or more elements
jaroslav@557: * in the specified collection are incompatible with this
jaroslav@557: * collection
jaroslav@557: * (optional)
jaroslav@557: * @throws NullPointerException if the specified collection contains one
jaroslav@557: * or more null elements and this collection does not permit null
jaroslav@557: * elements
jaroslav@557: * (optional),
jaroslav@557: * or if the specified collection is null.
jaroslav@557: * @see #contains(Object)
jaroslav@557: */
jaroslav@557: boolean containsAll(Collection> c);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Adds all of the elements in the specified collection to this collection
jaroslav@557: * (optional operation). The behavior of this operation is undefined if
jaroslav@557: * the specified collection is modified while the operation is in progress.
jaroslav@557: * (This implies that the behavior of this call is undefined if the
jaroslav@557: * specified collection is this collection, and this collection is
jaroslav@557: * nonempty.)
jaroslav@557: *
jaroslav@557: * @param c collection containing elements to be added to this collection
jaroslav@557: * @return true if this collection changed as a result of the call
jaroslav@557: * @throws UnsupportedOperationException if the addAll operation
jaroslav@557: * is not supported by this collection
jaroslav@557: * @throws ClassCastException if the class of an element of the specified
jaroslav@557: * collection prevents it from being added to this collection
jaroslav@557: * @throws NullPointerException if the specified collection contains a
jaroslav@557: * null element and this collection does not permit null elements,
jaroslav@557: * or if the specified collection is null
jaroslav@557: * @throws IllegalArgumentException if some property of an element of the
jaroslav@557: * specified collection prevents it from being added to this
jaroslav@557: * collection
jaroslav@557: * @throws IllegalStateException if not all the elements can be added at
jaroslav@557: * this time due to insertion restrictions
jaroslav@557: * @see #add(Object)
jaroslav@557: */
jaroslav@557: boolean addAll(Collection extends E> c);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes all of this collection's elements that are also contained in the
jaroslav@557: * specified collection (optional operation). After this call returns,
jaroslav@557: * this collection will contain no elements in common with the specified
jaroslav@557: * collection.
jaroslav@557: *
jaroslav@557: * @param c collection containing elements to be removed from this collection
jaroslav@557: * @return true if this collection changed as a result of the
jaroslav@557: * call
jaroslav@557: * @throws UnsupportedOperationException if the removeAll method
jaroslav@557: * is not supported by this collection
jaroslav@557: * @throws ClassCastException if the types of one or more elements
jaroslav@557: * in this collection are incompatible with the specified
jaroslav@557: * collection
jaroslav@557: * (optional)
jaroslav@557: * @throws NullPointerException if this collection contains one or more
jaroslav@557: * null elements and the specified collection does not support
jaroslav@557: * null elements
jaroslav@557: * (optional),
jaroslav@557: * or if the specified collection is null
jaroslav@557: * @see #remove(Object)
jaroslav@557: * @see #contains(Object)
jaroslav@557: */
jaroslav@557: boolean removeAll(Collection> c);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Retains only the elements in this collection that are contained in the
jaroslav@557: * specified collection (optional operation). In other words, removes from
jaroslav@557: * this collection all of its elements that are not contained in the
jaroslav@557: * specified collection.
jaroslav@557: *
jaroslav@557: * @param c collection containing elements to be retained in this collection
jaroslav@557: * @return true if this collection changed as a result of the call
jaroslav@557: * @throws UnsupportedOperationException if the retainAll operation
jaroslav@557: * is not supported by this collection
jaroslav@557: * @throws ClassCastException if the types of one or more elements
jaroslav@557: * in this collection are incompatible with the specified
jaroslav@557: * collection
jaroslav@557: * (optional)
jaroslav@557: * @throws NullPointerException if this collection contains one or more
jaroslav@557: * null elements and the specified collection does not permit null
jaroslav@557: * elements
jaroslav@557: * (optional),
jaroslav@557: * or if the specified collection is null
jaroslav@557: * @see #remove(Object)
jaroslav@557: * @see #contains(Object)
jaroslav@557: */
jaroslav@557: boolean retainAll(Collection> c);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes all of the elements from this collection (optional operation).
jaroslav@557: * The collection will be empty after this method returns.
jaroslav@557: *
jaroslav@557: * @throws UnsupportedOperationException if the clear operation
jaroslav@557: * is not supported by this collection
jaroslav@557: */
jaroslav@557: void clear();
jaroslav@557:
jaroslav@557:
jaroslav@557: // Comparison and hashing
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Compares the specified object with this collection for equality.
jaroslav@557: *
jaroslav@557: * While the Collection interface adds no stipulations to the
jaroslav@557: * general contract for the Object.equals, programmers who
jaroslav@557: * implement the Collection interface "directly" (in other words,
jaroslav@557: * create a class that is a Collection but is not a Set
jaroslav@557: * or a List) must exercise care if they choose to override the
jaroslav@557: * Object.equals. It is not necessary to do so, and the simplest
jaroslav@557: * course of action is to rely on Object's implementation, but
jaroslav@557: * the implementor may wish to implement a "value comparison" in place of
jaroslav@557: * the default "reference comparison." (The List and
jaroslav@557: * Set interfaces mandate such value comparisons.)
jaroslav@557: *
jaroslav@557: * The general contract for the Object.equals method states that
jaroslav@557: * equals must be symmetric (in other words, a.equals(b) if and
jaroslav@557: * only if b.equals(a)). The contracts for List.equals
jaroslav@557: * and Set.equals state that lists are only equal to other lists,
jaroslav@557: * and sets to other sets. Thus, a custom equals method for a
jaroslav@557: * collection class that implements neither the List nor
jaroslav@557: * Set interface must return false when this collection
jaroslav@557: * is compared to any list or set. (By the same logic, it is not possible
jaroslav@557: * to write a class that correctly implements both the Set and
jaroslav@557: * List interfaces.)
jaroslav@557: *
jaroslav@557: * @param o object to be compared for equality with this collection
jaroslav@557: * @return true if the specified object is equal to this
jaroslav@557: * collection
jaroslav@557: *
jaroslav@557: * @see Object#equals(Object)
jaroslav@557: * @see Set#equals(Object)
jaroslav@557: * @see List#equals(Object)
jaroslav@557: */
jaroslav@557: boolean equals(Object o);
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns the hash code value for this collection. While the
jaroslav@557: * Collection interface adds no stipulations to the general
jaroslav@557: * contract for the Object.hashCode method, programmers should
jaroslav@557: * take note that any class that overrides the Object.equals
jaroslav@557: * method must also override the Object.hashCode method in order
jaroslav@557: * to satisfy the general contract for the Object.hashCode method.
jaroslav@557: * In particular, c1.equals(c2) implies that
jaroslav@557: * c1.hashCode()==c2.hashCode().
jaroslav@557: *
jaroslav@557: * @return the hash code value for this collection
jaroslav@557: *
jaroslav@557: * @see Object#hashCode()
jaroslav@557: * @see Object#equals(Object)
jaroslav@557: */
jaroslav@557: int hashCode();
jaroslav@557: }
jaroslav@557: * String[] y = x.toArray(new String[0]);
jaroslav@557: *
jaroslav@557: * Note that toArray(new Object[0]) is identical in function to
jaroslav@557: * toArray().
jaroslav@557: *
jaroslav@557: * @param a the array into which the elements of this collection are to be
jaroslav@557: * stored, if it is big enough; otherwise, a new array of the same
jaroslav@557: * runtime type is allocated for this purpose.
jaroslav@557: * @return an array containing all of the elements in this collection
jaroslav@557: * @throws ArrayStoreException if the runtime type of the specified array
jaroslav@557: * is not a supertype of the runtime type of every element in
jaroslav@557: * this collection
jaroslav@557: * @throws NullPointerException if the specified array is null
jaroslav@557: */
jaroslav@557: