Interface Collection<E>
- All Superinterfaces:
Iterable<E>
- All Known Implementing Classes:
AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayDeque, ArrayList, ComponentSelector, HashSet, LinkedHashSet, LinkedList, PriorityQueue, Stack, TreeSet, Vector
Collection is the root of the collection hierarchy. It defines operations on
data collections and the behavior that they will have in all implementations
of Collections.
All direct or indirect implementations of Collection should implement at
least two constructors. One with no parameters which creates an empty
collection and one with a parameter of type Collection. This second
constructor can be used to create a collection of different type as the
initial collection but with the same elements. Implementations of Collection
cannot be forced to implement these two constructors but at least all
implementations under java.util do.
Methods that change the content of a collection throw an
UnsupportedOperationException if the underlying collection does not
support that operation, though it's not mandatory to throw such an Exception
in cases where the requested operation would not change the collection. In
these cases it's up to the implementation whether it throws an
UnsupportedOperationException or not.
Methods marked with (optional) can throw an
UnsupportedOperationException if the underlying collection doesn't
support that method.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanAttempts to addobjectto the contents of thisCollection(optional).booleanaddAll(Collection<? extends E> collection) Attempts to add all of the objects contained inCollectionto the contents of thisCollection(optional).voidclear()Removes all elements from thisCollection, leaving it empty (optional).booleanTests whether thisCollectioncontains the specified object.booleancontainsAll(Collection<?> collection) Tests whether thisCollectioncontains all objects contained in the specifiedCollection.booleanCompares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.inthashCode()Returns an integer hash code for the receiver.booleanisEmpty()Returns if thisCollectioncontains no elements.iterator()Returns an instance ofIteratorthat may be used to access the objects contained by thisCollection.booleanRemoves one instance of the specified object from thisCollectionif one is contained (optional).booleanremoveAll(Collection<?> collection) Removes all occurrences in thisCollectionof each object in the specifiedCollection(optional).booleanretainAll(Collection<?> collection) Removes all objects from thisCollectionthat are not also found in theCollectionpassed (optional).intsize()Returns a count of how many objects thisCollectioncontains.Object[]toArray()Returns a new array containing all elements contained in thisCollection.<T> T[]toArray(T[] array) Returns an array containing all elements contained in thisCollection.
-
Method Details
-
add
Attempts to add
objectto the contents of thisCollection(optional).After this method finishes successfully it is guaranteed that the object is contained in the collection.
If the collection was modified it returns
true,falseif no changes were made.An implementation of
Collectionmay narrow the set of accepted objects, but it has to specify this in the documentation. If the object to be added does not meet this restriction, then anIllegalArgumentExceptionis thrown.If a collection does not yet contain an object that is to be added and adding the object fails, this method must throw an appropriate unchecked Exception. Returning false is not permitted in this case because it would violate the postcondition that the element will be part of the collection after this method finishes.
Parameters
object: the object to add.
Returns
- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
addAll
Attempts to add all of the objects contained in
Collectionto the contents of thisCollection(optional). If the passedCollectionis changed during the process of adding elements to thisCollection, the behavior is not defined.Parameters
collection: theCollectionof objects.
Returns
- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisCollection. -
IllegalArgumentException: if an object cannot be added to thisCollection. -
NullPointerException: @throws NullPointerException ifcollectionisnull, or if it containsnullelements and thisCollectiondoes not support such elements.
-
-
clear
void clear()Removes all elements from this
Collection, leaving it empty (optional).Throws
UnsupportedOperationException: if removing from thisCollectionis not supported.
See also
-
#isEmpty
-
#size
-
contains
Tests whether this
Collectioncontains the specified object. Returnstrueif and only if at least one elementelemin thisCollectionmeets following requirement:(object==null ? elem==null : object.equals(elem)).Parameters
object: the object to search for.
Returns
- Returns:
trueif object is an element of thisCollection,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException if the object to look for isn't of the correct type. -
NullPointerException: @throws NullPointerException if the object to look for isnulland thisCollectiondoesn't supportnullelements.
-
-
containsAll
Tests whether this
Collectioncontains all objects contained in the specifiedCollection. If an elementelemis contained several times in the specifiedCollection, the method returnstrueeven ifelemis contained only once in thisCollection.Parameters
collection: the collection of objects.
Returns
- Returns:
trueif all objects in the specifiedCollectionare elements of thisCollection,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
equals
Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
Parameters
object: the object to compare with this object.
Returns
-
hashCode
-
isEmpty
boolean isEmpty()Returns if this
Collectioncontains no elements.Returns
- Returns:
trueif thisCollectionhas no elements,falseotherwise.See also
- #size
-
iterator
Returns an instance of
Iteratorthat may be used to access the objects contained by thisCollection. The order in which the elements are returned by the iterator is not defined. Only if the instance of theCollectionhas a defined order the elements are returned in that order.Returns
an iterator for accessing the
Collectioncontents. -
remove
Removes one instance of the specified object from this
Collectionif one is contained (optional). The elementelemthat is removed complies with(object==null ? elem==null : object.equals(elem).Parameters
object: the object to remove.
Returns
- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: if the object passed is not of the correct type. -
NullPointerException: @throws NullPointerException ifobjectisnulland thisCollectiondoesn't supportnullelements.
-
-
removeAll
Removes all occurrences in this
Collectionof each object in the specifiedCollection(optional). After this method returns none of the elements in the passedCollectioncan be found in thisCollectionanymore.Parameters
collection: the collection of objects to remove.
Returns
- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
retainAll
Removes all objects from this
Collectionthat are not also found in theCollectionpassed (optional). After this method returns thisCollectionwill only contain elements that also can be found in theCollectionpassed to this method.Parameters
collection: the collection of objects to retain.
Returns
- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
size
int size()Returns a count of how many objects this
Collectioncontains.Returns
- Returns:
- how many objects this
Collectioncontains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in thisCollection.
-
toArray
Object[] toArray()Returns a new array containing all elements contained in this
Collection.If the implementation has ordered elements it will return the element array in the same order as an iterator would return them.
The array returned does not reflect any changes of the
Collection. A new array is created even if the underlying data structure is already an array.Returns
an array of the elements from this
Collection. -
toArray
<T> T[] toArray(T[] array) Returns an array containing all elements contained in this
Collection. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than thisCollection, the array element following theCollectionelements is set to null.If the implementation has ordered elements it will return the element array in the same order as an iterator would return them.
toArray(new Object[0])behaves exactly the same way astoArray()does.Parameters
array: the array.
Returns
an array of the elements from this
Collection.Throws
ArrayStoreException: @throws ArrayStoreException if the type of an element in thisCollectioncannot be stored in the type of the specified array.
-