The Collection Interface
The Collection interface is the foundation upon which the Collections Framework is built
because it must be implemented by any class that defines a collection. Collection is a generic
interface that has this declaration:
interface Collection<E>
Here, E specifies the type of objects that the collection will hold. Collection extends the
Iterable interface. This means that all collections can be cycled through by use of the for-each
style for loop. (Recall that only classes that implement Iterable can be cycled through by the for.)
Collection declares the core methods that all collections will have. These methods are
summarized in Table 17-1. Because all collections implement Collection, familiarity with its
methods is necessary for a clear understanding of the framework. Several of these methods
can throw an UnsupportedOperationException. As explained, this occurs if a collection
cannot be modified. A ClassCastException is generated when one object is incompatible with
another, such as when an attempt is made to add an incompatible object to a collection. A
NullPointerException is thrown if an attempt is made to store a null object and null elements
are not allowed in the collection. An IllegalArgumentException is thrown if an invalid
argument is used. An IllegalStateException is thrown if an attempt is made to add an
element to a fixed-length collection that is full.
Objects are added to a collection by calling add( ). Notice that add( ) takes an argument
of type E, which means that objects added to a collection must be compatible with the type of
data expected by the collection. You can add the entire contents of one collection to another
by calling addAll( ).
You can remove an object by using remove( ). To remove a group of objects, call
removeAll( ). You can remove all elements except those of a specified group by calling
retainAll( ). To empty a collection, call clear( ).
You can determine whether a collection contains a specific object by calling contains( ).
To determine whether one collection contains all the members of another, call containsAll( ).
You can determine when a collection is empty by calling isEmpty( ). The number of elements
currently held in a collection can be determined by calling size( ).
The toArray( ) methods return an array that contains the elements stored in the invoking
collection. The first returns an array of Object. The second returns an array of elements that
have the same type as the array specified as a parameter. Normally, the second form is more
convenient because it returns the desired array type. These methods are more important than
it might at first seem. Often, processing the contents of a collection by using array-like syntax
is advantageous. By providing a pathway between collections and arrays, you can have the
best of both worlds.
Two collections can be compared for equality by calling equals( ). The precise meaning of
"equality" may differ from collection to collection. For example, you can implement equals( )
so that it compares the values of elements stored in the collection. Alternatively, equals( ) can
compare references to those elements.
One more very important method is iterator( ), which returns an iterator to a collection.
Iterators are frequently used when working with collections.
The List Interface
The List interface extends Collection and declares the behavior of a collection that stores a
sequence of elements. Elements can be inserted or accessed by their position in the list, using
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home