Collections Overview

The Java collections framework standardizes the way in which groups of objects are' handled by your programs. In the past, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties to store and manipulate groups of objects. Although these classes were quite useful, they lacked a central, unifying theme. Thus, , the way that you used Vector was different from the way that you used Properties, for example. Also, the previous, ad hoc approach was not designed to be easily extensible or adaptable. Collections are an answer to these (and other) problems. The collections framework was designed to meet several goals. First, the namc work had to be high-performance, The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) arc highly efficient. You seldom, if ever, need to code one of these data engines" manually, Second, the framework had to allow different types ,of collections to work in a similar manner and, with a high degree of interoperability. Third, extending and/or adapting a collection had to be easy. Toward this end, the entire collections framework is designed around a set of standard interfaces. Several standard implementations (such as LinkedList, HashSet, and TrecSet) of these interfaces are provided that soumya use as-is. You may also implement your own collection, if you choose. Various special-purpose implementations are created for your convenience, and some partial implementations are provided that make creating your ow n collection class easier. Finally, mechanisms were added that allow the integration of
standard arrays into the collections framework.
Algorithms are another important part of the collection mechanism. Algorithms operate on collections and are defined as static methods within the Collections class. Thus, they are available for all collections. Each collection class need not implement its own versions. -The algorithms provide a standard means of manipulating collections. Another item created by the collections framework is the Iterator interface. An iterator gives you a general-purpose, standardized way of accessing the elements within a collection, one at a time. US, an iterator provides a means of enumerating the contents of a collection. Because each collection implements Iterator, the clements cf any collection class can be accessed through the methods defined by Iterator, Thus, with only small changes, the code that cycles through a set can also be used to cycle through a list, for example:

In addition to collections, the framework defines several map interfaces and classes. Maps store key value pairs. Although maps arc not "collections" in the proper use of the term, they are fully integrated with collections. In the language of the collections - framework, you can obtain a collection-view of n map. Such a view contains the clements from the map stored in a collection. Thus, you can process the contents of a map as a collection, if you choose. The collection mechanism was retrofitted to some of the original classes defined by java.util so that they too could be integrated into the new system. It is important to understand that although the addition of collections has altered the architecture of ' many of the original utility classes, it has not caused the deprecation of any. Collections simply provide a better way of doing several things. One last thing: If you arc familiar with C++, then you will find it helpful to know  that the Java collections technology is similar in spirit to the Standard Template Library (STL) defined by C++. What C++ calls a container, Java calls a collection., The Collection Interfaces The collections framework defines several interfaces. This section provides an overview of each interface. Beginning with the collection interfaces is necessary because they determine the fundamental nature of the collection classes. Put differently, the concrete classes simply provide different implementations of the standard interfaces. The interfaces that underpin collections are summarized in the following table In addition to the collection interfaces, collections also use the Comparator, Iterator, and ListIterator interfaces, which are described in depth later in this chapter. Briefly

Comparator defines how two objects are compared; Herator and ListIterator enumerate the objects with a collection. To provide the greatest flexibility in their use, the collection interfaces allow some methods to be optional. The optional methods enable you to modify the contents of a collection. Collections that support these methods are called modifiable. Collections that do not allow their contents to be changed are called unmodifiable. If an attempt is made to use one of these methods on an unmodifiable collection an Unsupported Operation Exception is thrown. All the built-in collections are modifiable. The following sections examine the collection interfaces.

Share This