In addition to collections, the framework defines several map interfaces and classes. Maps
store key/value pairs. Although maps are part of the Collections Framework, they are not
"collections" in the strict use of the term. You can, however, obtain a collection-view of a map.
Such a view contains the elements 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 altered the architecture of many of the original utility
classes, it did not cause the deprecation of any. Collections simply provide a better way of
doing several things.
NOTE  If you are 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. However, there are significant differences between
the Collections Framework and the STL. It is important to not jump to conclusions.
Recent Changes to Collections
Recently, the Collections Framework underwent a fundamental change that significantly
increased its power and streamlined its use. The changes were caused by the addition of
generics, autoboxing/unboxing, and the for-each style for loop, by JDK 5. Although we will be
revisiting these topics throughout the course of this chapter, a brief overview is warranted now.
Generics Fundamentally Change the Collections Framework
The addition of generics caused a significant change to the Collections Framework because the
entire Collections Framework has been reengineered for it. All collections are now generic,
and many of the methods that operate on collections take generic type parameters. Simply
put, the addition of generics has affected every part of the Collections Framework.
Generics add the one feature that collections had been missing: type safety. Prior to generics,
all collections stored Object references, which meant that any collection could store any type
of object. Thus, it was possible to accidentally store incompatible types in a collection. Doing
so could result in run-time type mismatch errors. With generics, it is possible to explicitly
state the type of data being stored, and run-time type mismatch errors can be avoided.
Although the addition of generics changed the declarations of most of its classes and
interfaces, and several of their methods, overall, the Collections Framework still works the
same as it did prior to generics. However, if you are familiar with the pre-generics version
of the Collections Framework, you might find the new syntax a bit intimidating. Don't worry;
over time, the generic syntax will become second nature.
One other point: to gain the advantages that generics bring collections, older code will
need to be rewritten. This is also important because pre-generics code will generate warning
messages when compiled by a modern Java compiler. To eliminate these warnings, you will
need to add type information to all your collections code.
Autoboxing Facilitates the Use of Primitive Types
Autoboxing/unboxing facilitates the storing of primitive types in collections. As you will see,
a collection can store only references, not primitive values. In the past, if you wanted to store
a primitive value, such as an int, in a collection, you had to manually box it into its type
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks