|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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, and
Enumeration. 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 a
Collectionor aList:If the old API returns aFoo[] result = oldMethod(arg); newMethod(Arrays.asList(result));VectororHashtableyou have no work to do at all, sinceVectorhas been retrofitted to implement theListinterface, andHashtablehas been retrofitted to implementMap. Thus, aVectormay be passed directly to any method calling for aCollectionor aList:Similarly, aVector result = oldMethod(arg); newMethod(result);Hashtablemay be passed directly to any method calling for aMap:Less frequently, an API may return anHashtable result = oldMethod(arg); newMethod(result);Enumerationthat represents a collection of objects. While there is no direct support for translating anEnumerationinto aCollection, it's a simple matter to create aCollectioncontaining 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, theCollectioninterface contains atoArraymethod, 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));Hashtableis 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.enumerationmethod was provided to handle it. This method is a static factory that takes a
Collectionand returns anEnumerationover the elements of theCollection:Collection c = newMethod(); oldMethod(Collections.enumeration(c));
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.