Collection Basics

Interestingly enough, none of the Java books I have go into much detail on how to use the JDK collection classes, not even my own! So before we jump in and examine how we might model an XML document via a collection, it's important to understand exactly what collections are and how they can be used.

The JDK provides for what are commonly called collections. Collections, in the simplest sense, are just that: collections of objects. Collections are normally used to store, manipulate, and transmit data from one method to another. Examples of collections might be addresses in a phone book, cards in a playing hand, or any other natural grouping of objects. The JDK goes beyond simple collections and provides a framework for manipulating sets of objects. But what exactly is a framework? The American College Dictionary defines a framework as follows:

framework (fraym-wurk), n. (1) A structure composed of parts fitted and united together. (2) One designed to support or enclose something; frame or skeleton.

In the case of Java, the JDK provides a uniform architecture, or framework, for manipulating collections of objects. There are a number of benefits to providing the framework around which collections are developed. The most obvious benefit is that since all the collection classes are defined using uniform underlying principles they can all act and be acted on in similar ways. The JDK states a number of benefits to the collections framework. The most important are

  • Reduces development effort— The collection framework reduces development effort by providing a number of ready-to-use collections and algorithms that act on them.

  • Provides interoperability between unrelated APIs— By establishing a number of common interfaces that all collections must implement, classes can pass collections around and know in advance how to manipulate them.

  • Reduces the time required to learn the APIs— Since each collection is based on a uniform set of functionality, the learning curve for new collections is reduced.

And my two personal favorites:

  • Fosters code reuse— Since all collections implement a common set of APIs, we can develop algorithms that act on the interfaces and ignore the underlying details. We can reuse these classes again and again as new situations arise.

  • Reduces the effort required to design and implement APIs— Since the interfaces to the collection classes are well known, we need not agonize over how to implement something but can instead examine the details of only a specific implementation.

The framework sounds all well and good, but without some meat on these bones, it's all just fluff. The JDK provides that meat by way of a number of interfaces and some concrete implementations as well as some abstract classes. Specifically provided for are the following (each addresses in one way or another the important tenets of the collections framework):

  • Interfaces— The interfaces portion of the collections framework provides the supporting structure of our framework. Collections provide a number of interfaces for Sets, Lists, and Maps. The most basic, Collection, is a simple list of elements that might be ordered and might contain duplications. Sets are collections, still unordered, but cannot contain duplications. Lists may contain duplications but are ordered and allow for access by position. Maps are not truly collections but rather map from keys to values. Maps are ordered, via the keys, and cannot contain duplicates. Additionally, interfaces exist for SortedMap and SortedSets.

  • General-purpose implementation— The general-purpose implementations support a number of our framework ideas. The collection classes provide a set of ready-made implementations that a developer can use immediately. Six general-purpose implementations exist: HashSet, TreeSet, ArrayList, LinkedList, HashMap, and TreeMap.

  • Algorithms— Algorithms are provided by the JDK 1.2 for sorting, searching, shuffling, copying, and otherwise manipulating collections. All are provided via static methods on the Collection class.

  • Abstract implementations— In addition to general implementations, a number of partial implementations exist that developers can build on. Examples of these partial implementations are AbstractCollection, AbstractSet, and AbstractList.

  • Basic infrastructure— In addition to actual implementations and algorithms, a number of basic infrastructure classes exist for working with collections. We can move through a collection using Iterators. Iterators are similar to Enumerations but go beyond their basic functionality. We can compare collections using the Comparable and the Comparator interfaces, providing partial and complete orderings on collections.

  • Other aspects of the framework— The collections framework covers a number of other aspects as well, from special purpose implementations to convenience classes to support for legacy collections such as Vector and Hashtable.

All in all, the JDK does an excellent job of both defining the collections framework and providing a starting point for using and developing new collections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.44