Java Reference
In-Depth Information
You can add objects to a set and iterate over all the objects in a set. You can also check whether a given
object is a member of the set or not. For this reason you cannot have duplicate objects in a set — each object
in a set must be unique. Of course, you can remove a given object from a set, but only if you know what the
object is in the first place — in other words, if you have a reference to the object in the set.
There are variations on the basic set that I have described here. For example, sets can be ordered, so ob-
jects added to a set are inserted into a sequence ordered according to some comparison criterion. Such sets
require that the objects to be stored are of a type that implements the Comparable<T> interface.
Sequences
The linked list that you have already explored to some extent is an example of a more general type of col-
lection called a sequence . A primary characteristic of a sequence is that the objects are stored in a linear
fashion, not necessarily in any particular order, but organized in an arbitrary fixed sequence with a begin-
ning and an end. This contrasts with the set discussed in the previous section, where there may be no order at
all. An ordinary array is basically another example of a sequence, but it is more limited than the equivalent
collection because it holds a fixed number of elements.
Collections generally have the capability to expand to accommodate as many elements as necessary. The
Vector<T> type, for example, is an example of a sequence that provides similar functionality to an array,
but also has the capability to accommodate as many new elements as you want to add to it. Figure 14-2 il-
lustrates the organization of objects in the various types of sequence collections that you have available.
FIGURE 14-2
 
 
Search WWH ::




Custom Search