AbstractCollection

public abstract class AbstractCollection extends Object
implements Collection<E>
Known Direct Subclasses
Known Indirect Subclasses

This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.

To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.)

To implement a modifiable collection, the programmer must additionally override this class's add method (which otherwise throws an UnsupportedOperationException), and the iterator returned by the iterator method must additionally implement its remove method.

The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification.

The documentation for each non-abstract method in this class describes its implementation in detail. Each of these methods may be overridden if the collection being implemented admits a more efficient implementation.

This class is a member of the Java Collections Framework.

See Also

Protected Constructor Summary

AbstractCollection()
Sole constructor.

Public Method Summary

boolean
add(E e)
Ensures that this collection contains the specified element (optional operation).

This implementation always throws an UnsupportedOperationException.

boolean
addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation).

This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn.

void
clear()
Removes all of the elements from this collection (optional operation).

This implementation iterates over this collection, removing each element using the Iterator.remove operation.

boolean
contains(Object o)
Returns true if this collection contains the specified element.

This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.

boolean
containsAll(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection.

This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection.

boolean
isEmpty()
Returns true if this collection contains no elements.

This implementation returns size() == 0.

abstract Iterator<E>
iterator()
Returns an iterator over the elements contained in this collection.
boolean
remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation).

This implementation iterates over the collection looking for the specified element.

boolean
removeAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection (optional operation).

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection.

boolean
retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection.

abstract int
size()
Returns the number of 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.

This implementation returns an array containing all the elements returned by this collection's iterator in the same order, stored in consecutive elements of the array, starting with index 0.

Object[]
toArray()
Returns an array containing all of the elements in this collection.

This implementation returns an array containing all the elements returned by this collection's iterator, in the same order, stored in consecutive elements of the array, starting with index 0.