Java Reference
In-Depth Information
Collection Classes
In this chapter we'll look at the collection classes that are defined in the
java.util
package. These
provide you with a variety of ways for structuring and managing collections of objects in your programs.
In particular the collection classes enable you to deal with situations where you don't know in advance
how many objects you'll need to store, or where you need a bit more flexibility in the way in which you
access an object from a collection than the indexing mechanism provided by an array.
In this chapter you will learn:
What sets, lists, and maps are, and how they work
❑
What an
Iterator
object is used for
❑
Which container classes are available
❑
What a
Vector
is and how to use
Vector
objects in your programs
❑
How to manage
Vector
objects so that storing and retrieving elements is type safe
❑
What a
Stack
is and how you use it
❑
What
LinkedLists
are and how you use them
❑
How you store and retrieve objects in a hash table represented by a
HashMap
object
❑
How you can generate hash codes for your own class objects
❑
Understanding the Collection Classes
The
collection classes
in
java.util
support various ways for you to store and manage objects of any
kind in memory. They include a professional implementation of a linked list that we took so much
trouble to develop for ourselves back in Chapter 6. If 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 number, then look no further. You
get all this and more in the collection classes.