Java Reference
In-Depth Information
R
EVIEWING THE
E
XERCISES
ArrayList
and
HashMap
(and the older legacy classes,
Vector
and
Hashtable
) are
used quite commonly in Java development. These classes and the other collection
classes provide an easy-to-use facility for managing objects in memory. Collection
objects, like
ArrayList
and
HashMap
, really contain references to objects.
ArrayList
references objects in an ordered fashion, whereas
HashMap
references objects in the
keyed manner.
An important thing to realize from this exercise is that both collection objects,
the
ArrayList
and the
HashMap
, contain just the references to the
Pupil
objects. They
do not contain copies of the objects. So when you retrieve the
Tom Pupil
object
from the
HashMap
and change its grade, you are changing the one and only copy of
this object, as it exists in memory. When you display the list the second time, Tom's
grade is now an A because the
ArrayList
is referencing the same object that you re-
trieved from the
HashMap
.