Because of support for raw types, it is not necessary to immediately update older
collection code. However, all new code should use generics, and you should update older
code as soon as time permits. The addition of generics to the Collections Framework is a
fundamental improvement that should be utilized wherever possible.
The Legacy Classes and Interfaces
As explained at the start of this chapter, early versions of java.util did not include the
Collections Framework. Instead, it defined several classes and an interface that provided an
ad hoc method of storing objects. When collections were added (by J2SE 1.2), several of the
original classes were reengineered to support the collection interfaces. Thus, they are fully
compatible with the framework. While no classes have actually been deprecated, one has been
rendered obsolete. Of course, where a collection duplicates the functionality of a legacy class,
you will usually want to use the collection for new code. In general, the legacy classes are
supported because there is still code that uses them.
One other point: none of the collection classes are synchronized, but all the legacy classes
are synchronized. This distinction may be important in some situations. Of course, you can
easily synchronize collections, too, by using one of the algorithms provided by Collections.
The legacy classes defined by java.util are shown here:
Dictionar y
Proper ties
There is one legacy interface called Enumeration. The following sections examine Enumeration
and each of the legacy classes, in turn.
The Enumeration Interface
The Enumeration interface defines the methods by which you can enumerate (obtain one at
a time) the elements in a collection of objects. This legacy interface has been superseded by
Iterator. Although not deprecated, Enumeration is considered obsolete for new code. However,
it is used by several methods defined by the legacy classes (such as Vector and Properties),
is used by several other API classes, and is currently in widespread use in application code.
Because it is still in use, it was retrofitted for generics by JDK 5. It has this declaration:
interface Enumeration<E>
where E specifies the type of element being enumerated.
Enumeration specifies the following two methods:
boolean hasMoreElements( )
E nextElement( )
When implemented, hasMoreElements( ) must return true while there are still more elements
to extract, and false when all the elements have been enumerated. nextElement( ) returns the
next object in the enumeration. That is, each call to nextElement( ) obtains the next object in
the enumeration. It throws NoSuchElementException when the enumeration is complete.
Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector
is synchronized, and it contains many legacy methods that are not part of the Collections
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks