public abstract class AbstractCollection<E> extends Object implements Collection<E>
AbstractCollection is an abstract implementation of the Collection interface. A subclass must implement the abstract methods iterator() and size() to create an immutable collection. To create a
modifiable collection it's necessary to override the add() method that
currently throws an UnsupportedOperationException.| Modifier | Constructor and Description |
|---|---|
protected |
AbstractCollection()
Constructs a new instance of this AbstractCollection.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E object)
Ensures that this collection contains the specified element (optional
operation).
|
boolean |
addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained in
collection
to the contents of this Collection (optional). |
void |
clear()
Removes all elements from this
Collection, leaving it empty (optional). |
boolean |
contains(Object object)
Tests whether this
Collection contains the specified object. |
boolean |
containsAll(Collection<?> collection)
Tests whether this
Collection contains all objects contained in the
specified Collection. |
boolean |
isEmpty()
Returns if this
Collection contains no elements. |
abstract Iterator<E> |
iterator()
Returns an instance of
Iterator that may be used to access the
objects contained by this Collection. |
boolean |
remove(Object object)
Removes one instance of the specified object from this
Collection if one
is contained (optional). |
boolean |
removeAll(Collection<?> collection)
Removes all occurrences in this
Collection of each object in the
specified Collection (optional). |
boolean |
retainAll(Collection<?> collection)
Removes all objects from this
Collection that are not also found in the
Collection passed (optional). |
abstract int |
size()
Returns a count of how many objects this
Collection contains. |
Object[] |
toArray()
Returns an array containing all of the elements in this collection.
|
<T> T[] |
toArray(T[] contents)
Returns an array containing all of the elements in this collection;
the runtime type of the returned array is that of the specified array.
|
String |
toString()
Returns the string representation of this
Collection. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitequals, forEach, hashCode, parallelStream, removeIf, spliterator, stream, toArrayprotected AbstractCollection()
public boolean add(E object)
CollectionCollections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.
If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.
add in interface Collection<E>object - element whose presence in this collection is to be ensuredpublic boolean addAll(Collection<? extends E> collection)
collection
to the contents of this Collection (optional). This implementation
iterates over the given Collection and calls add for each
element. If any of these calls return true, then true is
returned as result of this method call, false otherwise. If this
Collection does not support adding elements, an UnsupportedOperationException is thrown.
If the passed Collection is changed during the process of adding elements
to this Collection, the behavior depends on the behavior of the passed
Collection.
addAll in interface Collection<E>collection - the collection of objects.true if this Collection is modified, false
otherwise.UnsupportedOperationException - if adding to this Collection is not supported.ClassCastException - if the class of an object is inappropriate for this
Collection.IllegalArgumentException - if an object cannot be added to this Collection.NullPointerException - if collection is null, or if it contains
null elements and this Collection does not support
such elements.Collection.add(Object)public void clear()
Collection, leaving it empty (optional).
This implementation iterates over this Collection and calls the remove method on each element. If the iterator does not support removal
of elements, an UnsupportedOperationException is thrown.
Concrete implementations usually can clear a Collection more efficiently
and should therefore overwrite this method.
clear in interface Collection<E>UnsupportedOperationException - it the iterator does not support removing elements from
this Collectioniterator(),
isEmpty(),
size()public boolean contains(Object object)
Collection contains the specified object. This
implementation iterates over this Collection and tests, whether any
element is equal to the given object. If object != null then
object.equals(e) is called for each element e returned by
the iterator until the element is found. If object == null then
each element e returned by the iterator is compared with the test
e == null.contains in interface Collection<E>object - the object to search for.true if object is an element of this Collection, false otherwise.ClassCastException - if the object to look for isn't of the correct type.NullPointerException - if the object to look for is null and this
Collection doesn't support null elements.public boolean containsAll(Collection<?> collection)
Collection contains all objects contained in the
specified Collection. This implementation iterates over the specified
Collection. If one element returned by the iterator is not contained in
this Collection, then false is returned; true otherwise.containsAll in interface Collection<E>collection - the collection of objects.true if all objects in the specified Collection are
elements of this Collection, false otherwise.ClassCastException - if one or more elements of collection isn't of the
correct type.NullPointerException - if collection contains at least one null
element and this Collection doesn't support null
elements.NullPointerException - if collection is null.Collection.contains(Object)public boolean isEmpty()
Collection contains no elements. This implementation
tests, whether size returns 0.isEmpty in interface Collection<E>true if this Collection has no elements, false
otherwise.size()public abstract Iterator<E> iterator()
Iterator that may be used to access the
objects contained by this Collection. The order in which the elements are
returned by the Iterator is not defined unless the instance of the
Collection has a defined order. In that case, the elements are returned in that order.
In this class this method is declared abstract and has to be implemented
by concrete Collection implementations.
public boolean remove(Object object)
Collection if one
is contained (optional). This implementation iterates over this
Collection and tests for each element e returned by the iterator,
whether e is equal to the given object. If object != null
then this test is performed using object.equals(e), otherwise
using object == null. If an element equal to the given object is
found, then the remove method is called on the iterator and
true is returned, false otherwise. If the iterator does
not support removing elements, an UnsupportedOperationException
is thrown.remove in interface Collection<E>object - the object to remove.true if this Collection is modified, false
otherwise.UnsupportedOperationException - if removing from this Collection is not supported.ClassCastException - if the object passed is not of the correct type.NullPointerException - if object is null and this Collection
doesn't support null elements.public boolean removeAll(Collection<?> collection)
Collection of each object in the
specified Collection (optional). After this method returns none of the
elements in the passed Collection can be found in this Collection
anymore.
This implementation iterates over this Collection and tests for each
element e returned by the iterator, whether it is contained in
the specified Collection. If this test is positive, then the remove method is called on the iterator. If the iterator does not
support removing elements, an UnsupportedOperationException is
thrown.
removeAll in interface Collection<E>collection - the collection of objects to remove.true if this Collection is modified, false
otherwise.UnsupportedOperationException - if removing from this Collection is not supported.ClassCastException - if one or more elements of collection isn't of the
correct type.NullPointerException - if collection contains at least one null
element and this Collection doesn't support null
elements.NullPointerException - if collection is null.Collection.remove(Object),
Collection.contains(Object)public boolean retainAll(Collection<?> collection)
Collection that are not also found in the
Collection passed (optional). After this method returns this Collection
will only contain elements that also can be found in the Collection
passed to this method.
This implementation iterates over this Collection and tests for each
element e returned by the iterator, whether it is contained in
the specified Collection. If this test is negative, then the remove method is called on the iterator. If the iterator does not
support removing elements, an UnsupportedOperationException is
thrown.
retainAll in interface Collection<E>collection - the collection of objects to retain.true if this Collection is modified, false
otherwise.UnsupportedOperationException - if removing from this Collection is not supported.ClassCastException - if one or more elements of collection
isn't of the correct type.NullPointerException - if collection contains at least one
null element and this Collection doesn't support
null elements.NullPointerException - if collection is null.Collection.remove(Object),
Collection.contains(Object)public abstract int size()
Collection contains.
In this class this method is declared abstract and has to be implemented
by concrete Collection implementations.
size in interface Collection<E>Collection contains, or Integer.MAX_VALUE
if there are more than Integer.MAX_VALUE elements in this
Collection.public Object[] toArray()
CollectionThe returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray in interface Collection<E>public <T> T[] toArray(T[] contents)
CollectionIf this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
Like the Collection.toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a collection known to contain only strings. The following code can be used to dump the collection into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().toArray in interface Collection<E>T - the runtime type of the array to contain the collectioncontents - the array into which the elements of this collection are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose.public String toString()
Collection. The presentation
has a specific format. It is enclosed by square brackets ("[]"). Elements
are separated by ', ' (comma and space).