Java Reference
In-Depth Information
We'll be exploring the following capabilities provided by the package:
Class/Interface
Description
The
Iterator
interface
Declares methods for iterating through elements of a set, one at a
time.
The
Vector
class
Supports an array-like structure for storing any type of object.
The number of objects that you can store in a
Vector
object
increases automatically as necessary.
The
Stack
class
Supports the storage of any type of object in a push down stack.
The
LinkedList
class
Supports the storage of any type of object in a doubly-linked list.
The
HashMap
class
Supports the storage of any type of object in a
hash table
,
sometimes called a
map
.
We'll start by looking in general terms at various possible types of collections for objects.
Collections of Objects
Back in Chapter 6 we put together a basic class defining a linked list. An object of type
LinkedList
represented an example of a
collection
of objects that could be of any type. A
collection
is used as a
generic term for any object that represents a set of objects grouped together by some means. A linked
list is only one of a number of ways of grouping a number of objects together in a collection.
There are three main types of collections:
sets
,
sequences
, and
maps
. Let's first get an understanding of
how these three types of collections work in principle, and then come back to look at the Java classes
that implement versions of these. One point I would like to emphasize about the following discussion is
that when we talk about a collection of objects, we mean a collection of references to objects. In Java
collections only stores references - 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 usually ordered
in any particular 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.