24.4. The Thread Class

 
[Page 713]

Chapter 22. Java Collections Framework

Mayan God Shel, Mexico. Photographer: Philip Coblentz. Courtesy Brand X Pictures.

Objectives

  • To describe the Java Collections Framework hierarchy ( §22.1).

  • To use the common methods defined in the Collection interface for operating sets and lists ( §22.2).

  • To use the Iterator interface to traverse a collection ( §22.3).

  • To use the JDK 1.5 foreach loop to replace an iterator for traversing a collection ( §22.3).

  • To discover the Set interface, and know how and when to use HashSet , LinkedHashSet , or TreeSet to store elements ( §22.3).

  • To compare elements using the Comparator interface ( §22.4).

  • To explore the List interface, and know how and when to use ArrayList or LinkedList to store elements ( §22.5).

  • To know how to use the static methods in the Collections class for lists and collections ( §22.6).

  • To distinguish Vector and ArrayList , and know how to use Vector and Stack ( §22.7).

  • To explore the relationships among Collection , Queue , LinkedList , and PriorityQueue and to create priority queues using the PriorityQueue class ( §22.8).

  • (Optional) To understand the differences between Collection and Map , and know how and when to use HashMap , LinkedHashMap , and TreeMap to store values associated with keys ( §22.9).

  • To obtain singleton sets, lists, and maps, and unmodifiable sets, lists, and maps using the static methods in the Collections class ( §22.10).


[Page 714]

22.1. Introduction

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.

Figure 22.1. Set and List are subinterfaces of Collection .

Figure 22.2. An instance of Map stores a group of objects and their associated keys.

Note

All the interfaces and classes defined in the Java Collections Framework are grouped in the java.util package.



[Page 715]

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.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net