Home : Course Map : Chapter 10 : Java :
The Collections Framework
JavaTech
Course Map
Chapter 10

Introduction
Vector/Enumeration
Hashtable,HashMap
   Properties
Collections

Iterator/ArrayList
Generics
Preferences API
  Demo 1
Concurrency Utils
Enumerated Type
Arrays Class
String Tools
  String
  StringBuffer
  StringBuilder
  StringTokenizer
  String.split()

Calendar,Date,Time
  Demo 2
  Demo 3

Other Utilities
Exercises

    Supplements
Performance
Benchmarks
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The Collections Framework was first added in Java 1.2. It includes the original Vector, Hashtable, and Properties classes from Java 1.0 and also adds several important classes designed to make handling of "collections" of objects much easier.

We referred to these collections in Chapter 1: Supplements as object containers (not to be confused with graphic components containers in Chapters 6 and 7). We do not have space to devote to a full discussion of all the interfaces and classes in the Collections Framework, but we do describe some of the basic functionality. To quote from the online documentation (ref. Collections Framework),

The collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance. It allows for interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse.

How does it do all that? Some of the increased performance of the Collections Framework comes about because of the use of non-synchronized versions of some of the original object container classes. For example, we've already seen the non-synchronized HashMap class that generally replaces the original Hashtable class when thread safety is not an issue. There is also a non-synchronized replacement for Vector called ArrayList.

In addition, the Collections Framework includes optimized high-performance implementations of several useful and important algorithms and data structures - sorting algorithms for instance. Since these are provided by the framework, you don't have to write them yourself.

Collections Framework Organization

The Collections Framework adds interfaces for several new container types. The root interface is Collection, which represents a group of objects. There are three main sub-interfaces - Set, List, and SortedSet.

A Set is a collection that has no duplicate elements. The order of appearance of elements in the Set is unspecified and may change.

A List is an ordered collection such that each element in the list has a distinct place in the list. An element in a list can be retrieved by its integer index, somewhat like an array. Most Lists permit duplicate elements.

A SortedSet is a Set stored in such a way that when you iterate through the SortedSet with an Iterator (see next section), the elements are returned in ascending "order."

These are interfaces, not concrete classes. The Collections Framework has several concrete implementations. For example, Stack, LinkedList, and ArrayList all implement List. A concrete SortedSet is TreeSet, and a concrete Set is HashSet. There are several other concrete implementations as well for specialized purposes. We illustrate only a few of these in this book.

There are two other base interfaces in the Collections Framework - Map and SortedMap. All maps use key/value pairs, as in the Hashtable seen above. Other concrete implementations of Map are the Properties and HashMap classes seen earlier. A concrete version of SortedMap is the TreeMap.

J2SE 5.0 adds the Queue interface to the Collections Framework, and the LinkedList class has been retrofitted to implement Queue. That means you can use a LinkedList as a plain List or as a Queue. Queues are useful for when you need first-in, first-out behavior, though some queues provide different orderings.

All Collections support add() and remove() methods to insert and remove elements. The Queue interface adds the preferred offer() and poll() methods. They are functionally equivalent to add() and remove() except for their behavior in exceptional situations. While add() fails by throwing an unchecked exception if the queue is full, offer() returns false. Similarly, remove() fails with an exception if the queue is empty while poll() returns null.

References & Web Resources

 

Latest update: Nov. 18, 2004

              Tech
ArbitaryPrecision
   BigInteger
  
BigDecimal
Bit Handling
Exercises

           Physics
Data Gen&Analysis

  Demo 1
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.