Start of Tutorial > Start of Trail > Start of Lesson | Search |
The Collection Framework was designed to ensure complete interoperability between the new collection interfaces and the types that have traditionally been used to represent collections:Vector
,Hashtable
, array, andEnumeration
. In this section, you'll learn how to transform traditional collections to new collections and vice-versa.
Suppose that you're using an API that returns traditional collections in tandem with another API that requires objects implementing the collection interfaces introduced in JDK 1.2. To make the two APIs interoperate smoothly, you'll have to transform the traditional collections into new collections. Luckily, the Collection Framework makes this easy.Suppose that the old API returns an array of objects, and the new API requires a
Collection
. As discussed in the implementations lesson, the collections framework has a convenience implementation that allows an array of objects to be viewed as aList
. UsingArrays.asList
, an array can be passed to any method requiring aCollection
or aList
:If the old API returns aFoo[] result = oldMethod(arg); newMethod(Arrays.asList(result));Vector
orHashtable
you have no work to do at all, sinceVector
has been retrofitted to implement theList
interface, andHashtable
has been retrofitted to implementMap
. Thus, 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. While there is no direct support for translating anEnumeration
into aCollection
, it's a simple matter to create aCollection
containing all the elements returned by anEnumeration
:Enumeration e = oldMethod(arg); List l = new ArrayList(); while (e.hasMoreElements()) l.add(e.nextElement()); newMethod(l);
Suppose you're using an API that returns "new collections" in tandem with another API that requires you to pass in "traditional collections". To make the two APIs interoperate smoothly, you have to transform the new collections into traditional collections. Again, the Collection Framework makes this easy. Gosh, isn't this your lucky day?Suppose that 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 some other type) instead of an array ofObject
? All hope is not lost. 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 method is a static factory that takes aCollection
and returns anEnumeration
over the elements of theCollection
:Collection c = newMethod(); oldMethod(Collections.enumeration(c));
Start of Tutorial > Start of Trail > Start of Lesson | Search |