Java Reference
In-Depth Information
You'll find that the collections framework is a major asset in most of your programs. When you want an
array that automatically expands to accommodate however many objects you throw into it, or you need to
be able to store and retrieve an object based on what it is rather than using an index or a sequence num-
ber, then look no further. You get all this and more in the generic types implemented within the collections
framework.
The collections framework involves too much for me to discuss it in complete detail, but you look at how
you apply some representative examples of collections that you're likely to need most often. You explore
the capabilities provided by the following generic types in detail:
• The Iterator<T> interface type declares methods for iterating through elements of a collection,
one at a time. You met this interface in the previous chapter.
• The Vector<T> type supports an array-like structure for storing objects. The number of objects
that you can store in a Vector<T> object increases automatically as necessary.
• The Stack<T> type supports the storage of objects in a pushdown stack.
• The LinkedList<T> type supports the storage of objects in a doubly-linked list, which is a list
that you can iterate through forward or backward.
• The EnumSet<E> type stores constants of a given enum type, E , in a set. You have already used
this collection class in file I/O operations.
• The HashMap<K,V> type supports the storage of objects of type V in a hash table, sometimes called
a map. An object is stored using an associated key object of type K . To retrieve an object you just
supply its associated key.
Let's start by looking in general terms at the various types of collections you can use.
COLLECTIONS OF OBJECTS
In Chapter 13 you put together a generic type that defined a linked list. An object of type LinkedList<T>
represented an example of a collection of objects of type T , where T could be any class or interface type
as long as the type met the conditions required by the collection. A collection is the term used to describe
any object that represents a set of objects grouped together and organized in a particular way in memory. A
class that defines collections of objects is often referred to as a container class . A linked list is just one of a
number of ways of organizing objects together in a collection.
There are three main groups of collections that organize objects in different ways, called sets , sequences ,
and maps . Let's first get an understanding of how these kinds of collections work in principle and then look
at the specific classes that implement versions of these. One point I'd like to emphasize about the following
discussion is that when I talk about a collection of objects, I mean a collection of references to objects. In
Java, collections store references only — the objects themselves are external to the collection.
Sets
A set is probably the simplest kind of collection you can have. Here the objects are not ordered in any par-
ticular way at all, and objects are simply added to the set without any control over where they go. It's a bit
like putting things in your pocket — you just put things in and they rattle around inside your pocket in no
particular order but at least you know roughly where they are. Figure 14-1 illustrates the idea of a set.
FIGURE 14-1
 
 
Search WWH ::




Custom Search