Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 681
نمايش فراداده

AbstractCollection<E>java.util

Java 1.2collection

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

o ); public boolean

containsAll (Collection<?>

c ); public boolean

isEmpty ( ); public abstract Iterator<E>

iterator ( ); public boolean

remove (Object

o ); public boolean

removeAll (Collection<?>

c ); public boolean

retainAll (Collection<?>

c ); public abstract int

size ( ); public Object[ ]

toArray ( ); public <T> T[ ]

toArray (T[ ]

a ); // Public Methods Overriding Object public String

toString ( ); }

Subclasses

AbstractList, AbstractQueue, AbstractSet