Collections
Collections
are a
fundamental idea in programming. Applications frequently need to keep
track of many related things, like a group of employees or a set of
images. To support the concept of many at a
fundamental level, of course, Java includes the concept of
arrays. Since a one-dimensional array has
a fixed length, arrays are awkward for sets of things that grow and
shrink over the lifetime of an application. Ever since SDK 1.0, the
Java platform has had two handy classes for keeping track of sets.
The
java.util.Vector
class represents a dynamic list of
objects, and the
java.util.Hashtable
class is a set of key/value pairs. The
Java 2 platform introduces a more comprehensive approach to
collections called the
Collections Framework. The
Vector and Hashtable classes
still exist, but they are now a part of the framework.
If you work with dictionaries or associative arrays in other languages, you should understand how useful these classes are. If you are someone who has worked in C or another static language, you should find collections to be truly magical. They are part of what makes Java powerful and dynamic. Being able to work with lists of objects and make associations between them is an abstraction from the details of the types. It lets you think about the problems at a higher level and saves you from having to reproduce common structures every time you need them.
The Collections Framework is based around a handful of interfaces in
the java.util package. These ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access