Selecting the Best Java Collection Class for Your Application
Java provides classes that implement the Collection interface to contain a collection of objects. There are over 20 of these collection classes, each having different performance and organizing properties.
This article explains how to choose the most appropriate of these classes for a particular use.
The Java interface java.util.Collection is implemented by classes and can contain or manage a collection of objects. Java provides over twenty concrete classes that implement the Collection interface. These classes differ in performance and how they organize objects. Some are for specialized purposes. If you are not familiar with the capabilities of these classes, deciding which one is most appropriate for a particular purpose can be a challenge.
Table 1 shows a list of the concrete classes that Java 1.6 provides.
The classes in the java.util and java.util.concurrent packages are general purpose and have a wide range of uses. In contrast, the classes in the other packages are very specialized and used only for specific purposes with other classes in their individual packages.
- The classes in the java.beans.beancontext package support bean contexts for JavaBeans development.
- The classes in the javax.management and javax.management.relation packages are management beans that support remote administrative interfaces for programs.
- The class in the javax.print.attribute.standard package is used by that package to help describe the current state of a print job.
That leaves 20 general-purpose classes to sort through. The most useful way to classify these is into two categories: how they organize objects, and how long they take to perform various operations. For most uses, how a Collection class organizes objects is more important than how quickly it can add, find, or remove them. Knowing that, let's focus on how these classes organize objects.