Name
AbstractCollection<E>
Synopsis
This
abstract class is a partial implementation of
Collection
that makes it easy to define custom
Collection
implementations. To create an
unmodifiable collection, simply override size(
)
and iterator( )
. The
Iterator
object returned by iterator(
)
has to support only the hasNext( )
and
next( )
methods. To define a modifiable
collection, you must additionally override the add(
)
method of
AbstractCollection
and make sure the
Iterator
returned by iterator(
)
supports the remove(
)
method. Some subclasses may choose
to override other methods to tune performance. In addition, it is
conventional that all subclasses provide two constructors: one that
takes no arguments and one that accepts a
Collection
argument that specifies the initial
contents of the collection.
Note that if you subclass AbstractCollection
directly, you are implementing a
bag
—an
unordered collection that allows duplicate
elements. If your add( )
method rejects duplicate
elements, you should subclass AbstractSet
instead.
See also AbstractList
.
Figure 16-1. java.util.AbstractCollection<E>
public abstract class AbstractCollection<E> implements Collection<E> { // Protected Constructors protected AbstractCollection( ); // Methods Implementing Collection public boolean add(E o); public boolean addAll(Collection<? extends E> c); public void clear( ); public boolean contains(Object
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.