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.asListto pass an array to any method requiring aCollectionor aList.If the old API returns aFoo[] result = oldMethod(arg); newMethod(Arrays.asList(result));Vectoror aHashtable, you have no work to do at all becauseVectorwas retrofitted to implement theListinterface, andHashtablewas retrofitted to implementMap. Therefore, 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. TheCollections.listmethod translates anEnumerationinto 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, theCollectioninterface contains atoArraymethod 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));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 is a static factory method that takes aCollectionand returns anEnumerationover the elements of theCollection.Collection c = newMethod(); oldMethod(Collections.enumeration(c));