Now that you have some set theory under your belt, you should be able to understand the Collections Framework more easily. The Collections Framework is made up of a set of interfaces
for working with groups of objects. The different interfaces
describe the different types of groups. For the most part, once
you understand the interfaces, you understand the framework.
While you always need to create specific implementations of the
interfaces, access to the actual collection should be restricted
to the use of the interface methods, thus allowing you to change
the underlying data structure, without altering the rest of your
code. The following diagrams shows the framework interface
hierarchy.
One might think that Map
would extend Collection
.
In mathematics, a map is just a collection of pairs. In the
Collections Framework, however, the interfaces Map
and Collection
are distinct with no lineage in the hierarchy. The reasons for
this distinction have to do with the ways that Set
and Map
are used in the Java libraries. The typical
application of a Map
is to provide access to values
stored by keys. The set of collection operations are all there, but you work with a key-value pair instead of an isolated element. Map
is therefore designed to support the
basic operations of get()
and put()
, which are
not required by Set
. Moreover, there are methods that
return Set
views of Map
objects:
Set set = aMap.keySet();
When designing software with the Collections Framework, it is
useful to remember the following hierarchical relationships of
the four basic interfaces of the framework:
- The
Collection
interface is a group of objects, with duplicates allowed. - The
Set
interface extends Collection
but forbids duplicates. - The
List
interface extends Collection
, allows duplicates, and introduces positional indexing. - The
Map
interface extends neither Set
nor Collection
.
Moving on to the framework implementations, the concrete
collection classes follow a naming convention, combining the
underlying data structure with the framework interface. The
following table shows the six collection implementations
introduced with the Java 2 framework, in addition to the four
historical collection classes. For information on how the
historical collection classes changed, like how Hashtable
was reworked into the framework, see the Historical collection classes.
Interface | Implementation | Historical |
---|
Set | HashSet | |
| TreeSet | |
List | ArrayList | Vector |
| LinkedList | Stack |
Map | HashMap | Hashtable |
| TreeMap | Properties |
There are no implementations of the Collection
interface. The historical collection
classes are called such because they have been around since the 1.0 release of
the Java class libraries.
If you are moving from the historical collection classes to
the new framework classes, one of the primary differences is that
all operations are unsynchronized with the new classes. While you
can add synchronization to the new classes, you cannot remove it
from the old.