Java Reference
In-Depth Information
add(anObject)
Sets
object1
anObject
object3
object4
object2
contains(object4)
Returns true since
object4 is in the set
The principal access mechanism that you have for a set is simply to 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 the
set must be unique. Of course, you can also 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 we have described here. For instance, sets can be ordered, so
objects added to a set will be inserted into a sequence of objects ordered according to some criterion of
comparison. Such sets require that the class that defines the objects to be stored implements suitable
methods for comparing objects.
Sequences
A linked list is an example of a more general type of collection called a sequence or a list . A primary
characteristic of a list is that the objects are stored in a linear fashion, not necessarily in any particular order,
with a beginning and an end. This contrasts with a set where there is no order at all. An ordinary array is
basically another example of a list, but is much more limited than a collection because it is fixed.
Collections generally have the ability to expand to accommodate as many elements as necessary. The
Vector class, for example, is a collection class that provides similar functionality to an array, but which
also has this ability to accommodate new elements as and when required.
Search WWH ::




Custom Search