Class AbstractCollection<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>
- Direct Known Subclasses:
AbstractList, AbstractQueue, AbstractSet, ArrayDeque
Class 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.
Since
1.2
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs a new instance of this AbstractCollection. -
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.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).abstract intsize()Returns a count of how many objects thisCollectioncontains.Object[]toArray()Returns a new array containing all elements contained in thisArrayList.<T> T[]toArray(T[] contents) Returns an array containing all elements contained in thisArrayList.toString()Returns the string representation of thisCollection.Methods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode
-
Constructor Details
-
AbstractCollection
protected AbstractCollection()Constructs a new instance of this AbstractCollection.
-
-
Method Details
-
add
Description copied from interface:CollectionAttempts 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
- Specified by:
addin interfaceCollection<E>- 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). This implementation iterates over the givenCollectionand callsaddfor each element. If any of these calls returntrue, thentrueis returned as result of this method call,falseotherwise. If thisCollectiondoes not support adding elements, anUnsupportedOperationExceptionis thrown.If the passed
Collectionis changed during the process of adding elements to thisCollection, the behavior depends on the behavior of the passedCollection.Parameters
collection: the collection of objects.
Returns
- Specified by:
addAllin interfaceCollection<E>- 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
public void clear()Removes all elements from this
Collection, leaving it empty (optional). This implementation iterates over thisCollectionand calls theremovemethod on each element. If the iterator does not support removal of elements, anUnsupportedOperationExceptionis thrown.Concrete implementations usually can clear a
Collectionmore efficiently and should therefore overwrite this method.Throws
UnsupportedOperationException: @throws UnsupportedOperationException it the iterator does not support removing elements from thisCollection
See also
-
#iterator
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>
-
contains
Tests whether this
Collectioncontains the specified object. This implementation iterates over thisCollectionand tests, whether any element is equal to the given object. Ifobject != nullthenobject.equals(e)is called for each elementereturned by the iterator until the element is found. Ifobject == nullthen each elementereturned by the iterator is compared with the teste == null.Parameters
object: the object to search for.
Returns
trueif object is an element of thisCollection,falseotherwise.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.
- Specified by:
containsin interfaceCollection<E>- 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. This implementation iterates over the specifiedCollection. If one element returned by the iterator is not contained in thisCollection, thenfalseis returned;trueotherwise.Parameters
collection: the collection of objects.
Returns
- Specified by:
containsAllin interfaceCollection<E>- 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.
-
-
isEmpty
public boolean isEmpty()Returns if this
Collectioncontains no elements. This implementation tests, whethersizereturns 0.Returns
- Specified by:
isEmptyin interfaceCollection<E>- 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 theIteratoris not defined unless the instance of theCollectionhas 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
Collectionimplementations.Returns
an iterator for accessing the
Collectioncontents. -
remove
Removes one instance of the specified object from this
Collectionif one is contained (optional). This implementation iterates over thisCollectionand tests for each elementereturned by the iterator, whethereis equal to the given object. Ifobject != nullthen this test is performed usingobject.equals(e), otherwise usingobject == null. If an element equal to the given object is found, then theremovemethod is called on the iterator andtrueis returned,falseotherwise. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.Parameters
object: the object to remove.
Returns
- Specified by:
removein interfaceCollection<E>- 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.This implementation iterates over this
Collectionand tests for each elementereturned by the iterator, whether it is contained in the specifiedCollection. If this test is positive, then theremovemethod is called on the iterator. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.Parameters
collection: the collection of objects to remove.
Returns
- Specified by:
removeAllin interfaceCollection<E>- 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.This implementation iterates over this
Collectionand tests for each elementereturned by the iterator, whether it is contained in the specifiedCollection. If this test is negative, then theremovemethod is called on the iterator. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.Parameters
collection: the collection of objects to retain.
Returns
- Specified by:
retainAllin interfaceCollection<E>- 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
public abstract int size()Returns a count of how many objects this
Collectioncontains.In this class this method is declared abstract and has to be implemented by concrete
Collectionimplementations.Returns
- Specified by:
sizein interfaceCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
toString
Returns the string representation of this
Collection. The presentation has a specific format. It is enclosed by square brackets ("[]"). Elements are separated by ', ' (comma and space).Returns
the string representation of this
Collection. -
toArray
Returns a new array containing all elements contained in this
ArrayList.Returns
an array of the elements from this
ArrayList- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] contents) Returns an array containing all elements contained in this
ArrayList. 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 thisArrayList, the array element following the collection elements is set to null.Parameters
contents: the array.
Returns
an array of the elements from this
ArrayList.Throws
ArrayStoreException: @throws ArrayStoreException when the type of an element in thisArrayListcannot be stored in the type of the specified array.
- Specified by:
toArrayin interfaceCollection<E>
-