There are convenience methods for converting from many of the
original collection classes and interfaces to the newer framework
classes and interfaces. They serve as bridges when you need a new
collection but have a historical collection. You can go from an
array or Vector
to a List
, a Hashtable
to a Map
, or an Enumeration
to any Collection
.
For going from any array to a List
, you use the asList(Object
array[])
method of the Arrays
class. Changes to the
List
pass through to the array, but you cannot adjust
the size of the array.
String names[] = {"Bernadine",
"Elizabeth", "Gene", "Clara"};
List list = Arrays.asList(names);
Because the original Vector
and Hashtable
classes have been retrofitted into the new framework, as a List
and Map
respectively, there is no work to treat either of
these historical collections as part of the new framework.
Treating a Vector
as a List
automatically
carries to its subclass Stack
. Treating a Hashtable
as a Map
automatically carries to its subclass Properties
.
Moving an Enumeration
to something in the new
framework requires a little more work, as nothing takes an Enumeration
in its constructor. So, to convert an Enumeration
, you
create some implementation class in the new framework and add
each element of the enumeration.
Enumeration enumeration = ...;
Collection collection = new LinkedList();
while (e.hasMoreElements()) {
collection.add(e.nextElement());
}
// Operate on collection