A collection is an object that is composed of elements. The elements may be stored
contiguously, that is, at consecutive locations in memory. Another option is a linked
structure, in which each element is stored in a special object called an entry that also
includes a reference to another entry.
A collection class is a class of which each instance is a collection. The Java Collections
Framework, part of the package java.util, includes a number of collection
classes that have wide applicability, including ArrayList, LinkedList, TreeMap, TreeSet,
HashMap, and HashSet. Each of those classes can be parameterized, which means
that the element class is specified when the collection-class object is created. And
for any instance of one of those classes, an iterator can be defined. An iterator is an
object that allows an instance of a collection class to loop through the elements in
that class without violating the Principle of Data Abstraction.
To simplify the programmer’s work of inserting elements into an instance of a
parameterized class, J2SE 1.5 automatically boxes primitive values into the corresponding
wrapper elements. Similarly, wrapper elements retrieved from a parameterclass
instance are automatically unboxed into the corresponding primitive value. A
further simplification of J2SE 1.5 is the enhanced for statement, which automates
most of the routine code to access each element during an iteration.
The framework consists of two hierarchies, one rooted at Collection and one
rooted at Map. The Collection interface consists of 15 method specifications for
accessing and manipulating a Collection object. The Collection hierarchy is extended
by the List and Set hierarchies.
The List interface adds several index-related methods to the Collection interface.
The List interface is partially implemented by the AbstractList class, and fully implemented
by the ArrayList and LinkedList classes.
The Set interface refines the Collection interface by allowing no duplicate elements.
The Set interface is partially implemented by the AbstractSet class, and fully
implemented in the TreeSet and HashSet classes.
A map is a collection in which each element has two parts: a unique key part
and a value part. The Java Collections Framework’s map hierarchy is rooted at the Map interface, partially implemented in the AbstractMap class, and fully implemented
in the TreeMap and HashMap classes.
To learn more about the book this website supports, please visit its Information Center.