Java Reference
In-Depth Information
Collections Framework
Packages
java.util
Use: J2SE (1.0; organized as an explicit API since JDK1.2)
Description
The Collections Framework allows developers to use features, such as dynamic resizing without writing all the
code themselves. The Collections API has changed since collections were first introduced in Java. The
Collections Framework aims to provide a more sophisticated way for programmers to deal with collections of
objects.
It's important to recognize that there's really only one way to store a group of items in the Java language—an
array. Arrays are a pretty basic kind of object storage. They provide a fixed set of references to objects. The data
type of all the references is set during array creation, and the size remains fixed throughout the array's existence.
The classes and interfaces used for the original collection capability in JDK1.0 were pretty basic. The JDK
provided just three concrete classes based on two kinds of collections:
Vector and Stack - Collections sorted with an integer index that provides absolute position with the collection.
Hashtable - A collection organized around key-value pairs. The key must be a unique Object, which is used to
locate the value. The value can be any Object, and is the element intended for storage in the Hashtable . For any
hash structure, the test for uniqueness is based on the return value of the hashCode() method. If two objects
return the same value for hashCode , they are assumed to be equal for the purposes of comparison for their keys.
We've come a long way since then. The modern Collections Framework consists of a set of ten concrete classes
that are built on top of an entire coordinating layer of interfaces and abstract classes. What's more, the framework
gives programmers a way to modify the functionality of the individual collections. Programmers can use the
java.util.Collections class to enhance an existing collection, for instance to synchronize an unsynchronized
collection.
When the development team created the Collections Framework for the JDK 1.2, they completely retrofitted the earlier collections so
that these “older” collections are part of the new model. It's fairly impressive that they managed to shoehorn the original classes into the
new framework, especially with so little change to the API. The team was able to update the model with little functional modification
and no deprecation.
Of course, there are a few differences between the original classes and the ones from the new model. The older
collection classes were designed to be thread safe from the start. So, if you look in the documentation for the
Hashtable , Stack , or Vector classes, you will see a lot of synchronized methods.
Are there synchronized methods in any of the modern collection classes? Nope. The new model uses collection
classes to provide basic storage functionality, and that's all. The Collections class provides methods to create
threadsafe versions of the collections. The example below shows how it's done:
Example 6.1 Collections class and threadsafe versions of collections
List internalList = new LinkedList();
List threadSafeList =
Collections.synchronizedList(internalList);
The LinkedList provides storage, and the synchronizedList method makes it into a synchronized collection.
Making synchronization available as an option is a real advantage for programmers. Basically, it means they don't
have to use synchronized collection code (with the associated performance hit) unless they really need the
capability.
As stated earlier, the Collections Framework is based on a set of interfaces. The API has two basic behavioral
chains. The first one is based on the Collection interface, which represents collections that perform simple
storage used to cross-reference the elements. The second is based on the Map interface, and describes collections
which are organized around key-value pairs.
Collection is the parent to three subinterfaces:
 
Search WWH ::




Custom Search