5 Changed the Collections Framework
When JDK 5 was released, some
fundamental changes were made to the Collections Framework that significantly
increased its power and streamlined its use. These changes include the addition
of generics, autoboxing/unboxing, and the for-each style for loop. Although JDK 8 is three major Java releases after JDK 5,
the effects of the JDK 5 features were so profound that they still warrant
special attention. The main reason is that you may encounter pre-JDK 5 code.
Understanding the effects and reasons for the changes is important if you will
be maintaining or updating older code.
Fundamentally Changed the Collections Framework
The addition of generics
caused a significant change to the Collections Framework because the entire Collections
Framework was 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 affected every part of the Collections Framework.
Generics added 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. Of course, 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.
Facilitates the Use of Primitive Types
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
wrapper. When the value was retrieved, it needed to be manually unboxed (by
using an explicit cast) into its proper primitive type. Because of
autoboxing/unboxing, Java can automatically perform the proper boxing and
unboxing needed when storing or retrieving primitive types. There is no need to
manually perform these operations.
For-Each Style for Loop
All collection classes in the Collections
Framework were retrofitted to implement the Iterable interface, which means that a collection can be cycled
through by use of the for-each style for
loop. In the past, cycling through a collection required the use of an iterator
(described later in this chapter), with the programmer manually constructing the
loop. Although iterators are still needed for some uses, in many cases,
iterator-based loops can be replaced by for