wrapper. When the value was retrieved, it needed to be manually unboxed (by using an
explicit cast) into its proper primitive type. Because of autoboxing/unboxing, Java can
automatically perform the proper boxing and unboxing needed when storing or retrieving
primitive types. There is no need to manually perform these operations.
The For-Each Style for Loop
All collection classes in the Collections Framework have been retrofitted to implement the
Iterable interface, which means that a collection can be cycled through by use of the for-each
style for loop. In the past, cycling through a collection required the use of an iterator (described
later in this chapter), with the programmer manually constructing the loop. Although iterators
are still needed for some uses, in many cases, iterator-based loops can be replaced by for loops.
The Collection Interfaces
The Collections Framework defines several interfaces. This section provides an overview of
each interface. Beginning with the collection interfaces is necessary because they determine
the fundamental nature of the collection classes. Put differently, the concrete classes simply
provide different implementations of the standard interfaces. The interfaces that underpin
collections are summarized in the following table:
Enables you to work with groups of objects; it is at the top of the collections
Extends Queue to handle a double-ended queue. (Added by Java SE 6.)
Extends Collection to handle sequences (lists of objects).
Extends Sor tedSet to handle retrieval of elements based on closest-match
searches. (Added by Java SE 6.)
Extends Collection to handle special types of lists in which elements are
removed only from the head.
Extends Collection to handle sets, which must contain unique elements.
Extends Set to handle sor ted sets.
In addition to the collection interfaces, collections also use the Comparator, RandomAccess,
Iterator, and ListIterator interfaces, which are described in depth later in this chapter. Briefly,
Comparator defines how two objects are compared; Iterator and ListIterator enumerate the
objects within a collection. By implementing RandomAccess, a list indicates that it supports
efficient, random access to its elements.
To provide the greatest flexibility in their use, the collection interfaces allow some methods
to be optional. The optional methods enable you to modify the contents of a collection.
Collections that support these methods are called modifiable. Collections that do not allow
their contents to be changed are called unmodifiable. If an attempt is made to use one of these
methods on an unmodifiable collection, an UnsupportedOperationException is thrown. All
the built-in collections are modifiable.
The following sections examine the collection interfaces.
Search WWH :