Objectives |
|
Chapter 20 introduced several data structures such as linked lists, stacks, queues, heaps, and priority queues. These popular data structures are widely used in many applications. Java provides an API known as the Collections Framework for these and many other useful data structures. So you can use them without having to reinvent the wheel.
The focus of this chapter is on how to use the classes and interfaces in the collections framework. Since knowledge of implementing data structures is not required for learning the Java Collections Framework, this chapter need not be used in conjunction with Chapter 20.
A collection is a container object that stores a group of objects, often referred to as elements . The Java Collections Framework supports three major types of collections: set , list , and map . They are defined in the interfaces Set , List , and Map . An instance of Set stores a group of nonduplicate elements. An instance of List stores an ordered collection of elements. An instance of Map stores a group of objects, each of which is associated with a key. The relationships of the major interfaces and classes in the Java Collections Framework are shown in Figures 22.1 and 22.2. These interfaces and classes provide a unified API for efficiently storing and processing a collection of objects. You will learn how to use these interfaces and classes in this chapter.
Note
All the interfaces and classes defined in the Java Collections Framework are grouped in the java.util package. |
Note
The design of the Java Collections Framework is a good example of using interfaces, abstract classes, and concrete classes. The interfaces define the framework. The abstract classes provide partial implementation for convenience. The concrete classes implement the interfaces with concrete data structures. |
Note
All the concrete classes in the Java Collections Framework implement the Cloneable and Serializable interfaces. Thus their instances can be cloned and serialized. |