Java Reference
In-Depth Information
21.1. Collections
Collections
(sometimes called
containers
) are holders that let you store
and organize objects in useful ways for efficient access. What will be effi-
cient depends on how you need to use the collection, so collections come
in many flavors. Most programming environments provide some collec-
tion types, ranging from impoverished up through gargantuan.
In the package
java.util
you will find interfaces and classes that provide
a generic
collection
framework. This framework gives you a consistent
and flexible set of collection interfaces and several useful implementa-
tions of these interfaces. You've already been briefly introduced to some
of these, such as the interfaces
List
,
Set
,
Map
, and
Iterator
, and imple-
mentations
ArrayList
and
HashMap
.
The collection framework is designed to be concise. The principle is to
have a core set of valuable collection abstractions and implementations
that are broadly useful, rather than an exhaustive set that is complete
but conceptually complex and unwieldy.
One way to keep the size down is to represent broad abstractions in
the interfaces rather than fine-grained differences. Notions such as im-
mutability and resizability are not represented by different interface
types. The core collection interfaces provide methods that allow all
common operations, leaving it to specific implementations to refuse
to execute particular improper operations by throwing the unchecked
java.lang.UnsupportedOperationException
.
Figure 21-1
shows the collections interfaces and the concrete implement-
[1]
The full list of collections includes abstract collections that help you write your own implementations.
These are presented in
Section 21.14
on page
611
. Other specialized implementations are also elided
for clarity, but are discussed later in the chapter.
•
Collection<E>
The root interface for collections. Provides such
methods as
add
,
remove
,
size
, and
toArray
.