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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access