The JavaTM Tutorial

| Start of Tutorial > Start of Trail | Search Feedback Form

Trail: Collections

## Lesson: Introduction

### What Is a Collection?

> A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another. Collections typically represent data items that form a natural group, like a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a collection of name-to-phone-number mappings).

> If you've used Java -- or just about any other programming language -- you're already familiar with collections. Collection implementations in earlier versions of Java included `Vector` , `Hashtable` , and array. While earlier versions of Java contained collection implementations, they did not contain a collections framework.

### What Is a Collections Framework?

> A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain three things:
> - Interfaces: abstract data types representing collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages like Java, these interfaces generally form a hierarchy.
> - Implementations: concrete implementations of the collection interfaces. In essence, these are reusable data structures.
> - Algorithms: methods that perform useful computations, like searching and sorting, on objects that implement collection interfaces. These algorithms are said to be polymorphic because the same method can be used on many different implementations of the appropriate collections interface. In essence, algorithms are reusable functionality.
> The best known examples of collections frameworks are the C++ Standard Template Library (STL), and Smalltalk's collection classes.

### What are the Benefits of a Collections Framework?

> - It reduces programming effort: By providing useful data structures and algorithms, a collections framework frees you to concentrate on the important parts of your program, rather than the low-level plumbing required to make it work. By facilitating interoperability among unrelated APIs (as described below), the collections framework frees you from writing oodles of adapter objects or conversion code to connect APIs.
> - It increases program speed and quality: The collections framework does this primarily by providing high-performance, high-quality implementations of useful data structures and algorithms. Also, because the various implementations of each interface are interchangeable, programs can be easily tuned by switching collection implementations. Finally, because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving the quality and performance of the rest of the program.
> - It allows interoperability among unrelated APIs: The collections interfaces will become the "lingua franca" by which APIs pass collections back and forth. If my network administration API furnishes a `Collection` of node names, and your GUI toolkit expects a Collection of column headings, our APIs will interoperate seamlessly even though they were written independently.
> - It reduces the effort to learn and use new APIs: Many APIs naturally take collections on input and output. In the past, each such API had a little "sub-API" devoted to manipulating its collections. There was little consistency among these ad-hoc collections sub-APIs, so you had to learn each one from scratch and it was easy to make mistakes when using them. With the advent of standard collections interfaces, the problem goes away.
> - It reduces effort to design new APIs: This is the flip-side of the previous advantage: designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections. They just use the standard collections interfaces.
> - It fosters software reuse: New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for new algorithms that operate on objects that implement these interfaces.

### Are there any Drawbacks?

> Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. We believe that Java's new collections framework breaks with this tradition, as you will learn for yourself in the following lessons.

| Start of Tutorial > Start of Trail | Search Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.