The Java Collections Framework was designed to ensure complete interoperability between the core collection interfaces and the types that were used to represent collections in the early versions of the Java platform:Vector
,Hashtable
, array, andEnumeration
. In this section, you'll learn how to transform old collections to the Java Collections Framework collections and vice versa.
Suppose that you're using an API that returns legacy collections in tandem with another API that requires objects implementing the collection interfaces. To make the two APIs interoperate smoothly, you'll have to transform the legacy collections into modern collections. Luckily, the Java Collections Framework makes this easy.Suppose the old API returns an array of objects and the new API requires a
Collection
. The Collections Framework has a convenience implementation that allows an array of objects to be viewed as aList
. You useArrays.asList
to pass an array to any method requiring aCollection
or aList
.If the old API returns aFoo[] result = oldMethod(arg); newMethod(Arrays.asList(result));Vector
or aHashtable
, you have no work to do at all becauseVector
was retrofitted to implement theList
interface, andHashtable
was retrofitted to implementMap
. Therefore, aVector
may be passed directly to any method calling for aCollection
or aList
.Similarly, aVector result = oldMethod(arg); newMethod(result);Hashtable
may be passed directly to any method calling for aMap
.Less frequently, an API may return anHashtable result = oldMethod(arg); newMethod(result);Enumeration
that represents a collection of objects. TheCollections.list
method translates anEnumeration
into aCollection
.Enumeration e = oldMethod(arg); newMethod(Collections.list(e));
Suppose you're using an API that returns modern collections in tandem with another API that requires you to pass in legacy collections. To make the two APIs interoperate smoothly, you have to transform modern collections into old collections. Again, the Java Collections Framework makes this easy.Suppose the new API returns a
Collection
, and the old API requires an array ofObject
. As you're probably aware, theCollection
interface contains atoArray
method designed expressly for this situation.What if the old API requires an array ofCollection c = newMethod(); oldMethod(c.toArray());String
(or another type) instead of an array ofObject
? You just use the other form oftoArray
the one that takes an array on input.If the old API requires aCollection c = newMethod(); oldMethod((String[]) c.toArray(new String[0]));Vector
, the standard collection constructor comes in handy.The case where the old API requires aCollection c = newMethod(); oldMethod(new Vector(c));Hashtable
is handled analogously.Finally, what do you do if the old API requires anMap m = newMethod(); oldMethod(new Hashtable(m));Enumeration
? This case isn't common, but it does happen from time to time, and theCollections.enumeration
method was provided to handle it. This is a static factory method that takes aCollection
and returns anEnumeration
over the elements of theCollection
.Collection c = newMethod(); oldMethod(Collections.enumeration(c));