Class AbstractCollection<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>
- Direct Known Subclasses:
AbstractList,AbstractQueue,AbstractSet,ArrayDeque,ConcurrentLinkedDeque
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.- Since:
- 1.2
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractCollection()Constructs a new instance of this AbstractCollection. -
Method Summary
Modifier and Type Method Description booleanadd(E object)Attempts 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).booleancontains(Object object)Tests whether thisCollectioncontains the specified object.booleancontainsAll(Collection<?> collection)Tests whether thisCollectioncontains all objects contained in the specifiedCollection.booleanisEmpty()Returns if thisCollectioncontains no elements.abstract Iterator<E>iterator()Returns an instance ofIteratorthat may be used to access the objects contained by thisCollection.booleanremove(Object object)Removes 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 thisCollection.<T> T[]toArray(T[] contents)Returns an array containing all elements contained in thisCollection.StringtoString()Returns the string representation of thisCollection.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode
-
Constructor Details
-
AbstractCollection
protected AbstractCollection()Constructs a new instance of this AbstractCollection.
-
-
Method Details
-
add
Description copied from interface:CollectionAttempts to addobjectto 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,falseif no changes were made. An implementation ofCollectionmay 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.- Specified by:
addin interfaceCollection<E>- Parameters:
object- the object to add.- Returns:
trueif thisCollectionis modified,falseotherwise.
-
addAll
Attempts to add all of the objects contained incollectionto 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.- Specified by:
addAllin interfaceCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif thisCollectionis modified,falseotherwise.- Throws:
UnsupportedOperationException- if adding to thisCollectionis not supported.ClassCastException- if the class of an object is inappropriate for thisCollection.IllegalArgumentException- if an object cannot be added to thisCollection.NullPointerException- ifcollectionisnull, or if it containsnullelements and thisCollectiondoes not support such elements.
-
clear
public void clear()Removes all elements from thisCollection, 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.- Specified by:
clearin interfaceCollection<E>- Throws:
UnsupportedOperationException- it the iterator does not support removing elements from thisCollection- See Also:
iterator(),isEmpty(),size()
-
contains
Tests whether thisCollectioncontains 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.- Specified by:
containsin interfaceCollection<E>- 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- if the object to look for isnulland thisCollectiondoesn't supportnullelements.
-
containsAll
Tests whether thisCollectioncontains 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.- Specified by:
containsAllin interfaceCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif all objects in the specifiedCollectionare elements of thisCollection,falseotherwise.- Throws:
ClassCastException- if one or more elements ofcollectionisn't of the correct type.NullPointerException- ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements.NullPointerException- ifcollectionisnull.
-
isEmpty
public boolean isEmpty()Returns if thisCollectioncontains no elements. This implementation tests, whethersizereturns 0.- Specified by:
isEmptyin interfaceCollection<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.- See Also:
size()
-
iterator
Returns an instance ofIteratorthat 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. -
remove
Removes one instance of the specified object from thisCollectionif 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.- Specified by:
removein interfaceCollection<E>- Parameters:
object- the object to remove.- 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- ifobjectisnulland thisCollectiondoesn't supportnullelements.
-
removeAll
Removes all occurrences in thisCollectionof 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.- Specified by:
removeAllin interfaceCollection<E>- Parameters:
collection- the collection of objects to remove.- Returns:
trueif thisCollectionis modified,falseotherwise.- Throws:
UnsupportedOperationException- if removing from thisCollectionis not supported.ClassCastException- if one or more elements ofcollectionisn't of the correct type.NullPointerException- ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements.NullPointerException- ifcollectionisnull.
-
retainAll
Removes all objects from thisCollectionthat 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.- Specified by:
retainAllin interfaceCollection<E>- Parameters:
collection- the collection of objects to retain.- Returns:
trueif thisCollectionis modified,falseotherwise.- Throws:
UnsupportedOperationException- if removing from thisCollectionis not supported.ClassCastException- if one or more elements ofcollectionisn't of the correct type.NullPointerException- ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements.NullPointerException- ifcollectionisnull.
-
size
public abstract int size()Returns a count of how many objects thisCollectioncontains.In this class this method is declared abstract and has to be implemented by concrete
Collectionimplementations.- Specified by:
sizein interfaceCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
toArray
Description copied from interface:CollectionReturns 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.- Specified by:
toArrayin interfaceCollection<E>- Returns:
- an array of the elements from this
Collection.
-
toArray
public <T> T[] toArray(T[] contents)Description copied from interface:CollectionReturns 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 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.- Specified by:
toArrayin interfaceCollection<E>- Parameters:
contents- the array.- Returns:
- an array of the elements from this
Collection.
-
toString
Returns the string representation of thisCollection. The presentation has a specific format. It is enclosed by square brackets ("[]"). Elements are separated by ', ' (comma and space).
-