Package java.util
Interface Collection<E>
-
- All Superinterfaces:
Iterable<E>
- All Known Implementing Classes:
ArrayList
public interface Collection<E> extends Iterable<E>
Collection
is the root of the collection hierarchy. It defines operations on data collections and the behavior that they will have in all implementations ofCollection
s. All direct or indirect implementations ofCollection
should implement at least two constructors. One with no parameters which creates an empty collection and one with a parameter of typeCollection
. This second constructor can be used to create a collection of different type as the initial collection but with the same elements. Implementations ofCollection
cannot be forced to implement these two constructors but at least all implementations underjava.util
do. Methods that change the content of a collection throw anUnsupportedOperationException
if the underlying collection does not support that operation, though it's not mandatory to throw such anException
in cases where the requested operation would not change the collection. In these cases it's up to the implementation whether it throws anUnsupportedOperationException
or not. Methods marked with (optional) can throw anUnsupportedOperationException
if the underlying collection doesn't support that method.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description boolean
add(E object)
Attempts to addobject
to the contents of thisCollection
(optional).boolean
addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained inCollection
to the contents of thisCollection
(optional).void
clear()
Removes all elements from thisCollection
, leaving it empty (optional).boolean
contains(Object object)
Tests whether thisCollection
contains the specified object.boolean
containsAll(Collection<?> collection)
Tests whether thisCollection
contains all objects contained in the specifiedCollection
.boolean
equals(Object object)
Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.int
hashCode()
Returns an integer hash code for the receiver.boolean
isEmpty()
Returns if thisCollection
contains no elements.Iterator<E>
iterator()
Returns an instance ofIterator
that may be used to access the objects contained by thisCollection
.boolean
remove(Object object)
Removes one instance of the specified object from thisCollection
if one is contained (optional).boolean
removeAll(Collection<?> collection)
Removes all occurrences in thisCollection
of each object in the specifiedCollection
(optional).boolean
retainAll(Collection<?> collection)
Removes all objects from thisCollection
that are not also found in theCollection
passed (optional).int
size()
Returns a count of how many objects thisCollection
contains.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 Detail
-
add
boolean add(E object)
Attempts to addobject
to 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 returnstrue
,false
if no changes were made. An implementation ofCollection
may 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 anIllegalArgumentException
is 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:
true
if thisCollection
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if adding to thisCollection
is not supported.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
boolean addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained inCollection
to the contents of thisCollection
(optional). If the passedCollection
is changed during the process of adding elements to thisCollection
, the behavior is not defined.- Parameters:
collection
- theCollection
of objects.- Returns:
true
if thisCollection
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if adding to thisCollection
is not supported.ClassCastException
- if the class of an object is inappropriate for thisCollection
.IllegalArgumentException
- if an object cannot be added to thisCollection
.NullPointerException
- ifcollection
isnull
, or if it containsnull
elements and thisCollection
does not support such elements.
-
clear
void clear()
Removes all elements from thisCollection
, leaving it empty (optional).
-
contains
boolean contains(Object object)
Tests whether thisCollection
contains the specified object. Returnstrue
if and only if at least one elementelem
in thisCollection
meets following requirement:(object==null ? elem==null : object.equals(elem))
.- Parameters:
object
- the object to search for.- Returns:
true
if object is an element of thisCollection
,false
otherwise.- Throws:
ClassCastException
- if the object to look for isn't of the correct type.NullPointerException
- if the object to look for isnull
and thisCollection
doesn't supportnull
elements.
-
containsAll
boolean containsAll(Collection<?> collection)
Tests whether thisCollection
contains all objects contained in the specifiedCollection
. If an elementelem
is contained several times in the specifiedCollection
, the method returnstrue
even ifelem
is contained only once in thisCollection
.- Parameters:
collection
- the collection of objects.- Returns:
true
if all objects in the specifiedCollection
are elements of thisCollection
,false
otherwise.- Throws:
ClassCastException
- if one or more elements ofcollection
isn't of the correct type.NullPointerException
- ifcollection
contains at least onenull
element and thisCollection
doesn't supportnull
elements.NullPointerException
- ifcollection
isnull
.
-
equals
boolean equals(Object object)
Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.- Overrides:
equals
in classObject
- Parameters:
object
- the object to compare with this object.- Returns:
true
if the object is the same as this object andfalse
if it is different from this object.- See Also:
hashCode()
-
hashCode
int hashCode()
Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.- Overrides:
hashCode
in classObject
- Returns:
- the receiver's hash.
- See Also:
equals(java.lang.Object)
-
isEmpty
boolean isEmpty()
Returns if thisCollection
contains no elements.- Returns:
true
if thisCollection
has no elements,false
otherwise.- See Also:
size()
-
iterator
Iterator<E> iterator()
Returns an instance ofIterator
that 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 theCollection
has a defined order the elements are returned in that order.
-
remove
boolean remove(Object object)
Removes one instance of the specified object from thisCollection
if one is contained (optional). The elementelem
that is removed complies with(object==null ? elem==null : object.equals(elem)
.- Parameters:
object
- the object to remove.- Returns:
true
if thisCollection
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if removing from thisCollection
is not supported.ClassCastException
- if the object passed is not of the correct type.NullPointerException
- ifobject
isnull
and thisCollection
doesn't supportnull
elements.
-
removeAll
boolean removeAll(Collection<?> collection)
Removes all occurrences in thisCollection
of each object in the specifiedCollection
(optional). After this method returns none of the elements in the passedCollection
can be found in thisCollection
anymore.- Parameters:
collection
- the collection of objects to remove.- Returns:
true
if thisCollection
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if removing from thisCollection
is not supported.ClassCastException
- if one or more elements ofcollection
isn't of the correct type.NullPointerException
- ifcollection
contains at least onenull
element and thisCollection
doesn't supportnull
elements.NullPointerException
- ifcollection
isnull
.
-
retainAll
boolean retainAll(Collection<?> collection)
Removes all objects from thisCollection
that are not also found in theCollection
passed (optional). After this method returns thisCollection
will only contain elements that also can be found in theCollection
passed to this method.- Parameters:
collection
- the collection of objects to retain.- Returns:
true
if thisCollection
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if removing from thisCollection
is not supported.ClassCastException
- if one or more elements ofcollection
isn't of the correct type.NullPointerException
- ifcollection
contains at least onenull
element and thisCollection
doesn't supportnull
elements.NullPointerException
- ifcollection
isnull
.
-
size
int size()
Returns a count of how many objects thisCollection
contains.- Returns:
- how many objects this
Collection
contains, 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 thisCollection
. 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 theCollection
. 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 thisCollection
. 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 theCollection
elements 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.- Type Parameters:
T
- the type.- Parameters:
array
- the array.- Returns:
- an array of the elements from this
Collection
. - Throws:
ArrayStoreException
- if the type of an element in thisCollection
cannot be stored in the type of the specified array.
-
-