Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Collection 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, and Enumeration. In this section, you'll learn how to transform old collections to 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 Collections Framework makes this easy.Suppose that the old API returns an array of objects and that the new API requires a Collection. The Collections Framework has a convenience implementation that allows an array of objects to be viewed as a List. You use Suppose that the old API returns an array of objects and that the new API requires a Collection. The Collections Framework has a convenience implementation that allows an array of objects to be viewed as a List. You use Arrays.asList to pass an array to any method requiring a Collection or a List:
If the old API returns a Vector or a Hashtable, you have no work to do at all, because Vector was retrofitted to implement the List interface, and Hashtable was retrofitted to implement Map. Therefore, a Vector may be passed directly to any method calling for a Collection or a List:Foo[] result = oldMethod(arg); newMethod(Arrays.asList(result));Similarly, a Hashtable may be passed directly to any method calling for a Map:Vector result = oldMethod(arg); newMethod(result);Less frequently, an API may return an Enumeration that represents a collection of objects. The Collections.list method translates an Enumeration into a Collection:Hashtable result = oldMethod(arg); newMethod(result);Enumeration e = oldMethod(arg); newMethod(Collections.list(e));
Suppose that 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 the modern collections into old collections. Again, the Collections Framework makes this easy.Suppose that the new API returns a Collection, and the old API requires an array of Object. As you're probably aware, the Collection interface contains a toArray method designed expressly for this situation:
What if the old API requires an array of String (or another type) instead of an array of Object? You just use the other form of toArray, the one that takes an array on input:Collection c = newMethod(); oldMethod(c.toArray());If the old API requires a Vector, the standard collection constructor comes in handy:Collection c = newMethod(); oldMethod((String[]) c.toArray(new String[0]));The case where the old API requires a Hashtable is handled analogously:Collection c = newMethod(); oldMethod(new Vector(c));Finally, what do you do if the old API requires an Enumeration? This case isn't common, but it does happen from time to time, and the Collections.enumeration method was provided to handle it. This method is a static factory that takes a Collection and returns an Enumeration over the elements of the Collection:Map m = newMethod(); oldMethod(new Hashtable(m));Collection c = newMethod(); oldMethod(Collections.enumeration(c));
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.