Interoperability (Interoperability)
In this course, you will learn about the interoperability of two aspects:
Compatibility
This course will show you how to make the set of objects work with the old APIs that were added to the Java platform before the set of objects.
API Design
teaches you how to design a new API so that it can interoperate seamlessly.
Compatibility
The collection Framework is designed to ensure complete interoperability between the new object set interface and the traditional type used to represent the set of objects: vectors, Hashtable, array, and enumeration. In this section, you will learn how to transform a traditional set of objects into a new set of objects and the opposite process.
Up compatibility
Let's say you want to use an API that will return a traditional set of objects, and use another API, which requires the object to implement the JDK1.2 object set interface. To allow these two APIs to interoperate smoothly, you must convert the traditional set of objects into a new set of objects. Fortunately, the Collection Framework makes this work very simple.
Suppose the old API returns an array of objects, and the new API requires a Collection. As discussed in the implementation course, the object set schema has a convenient implementation that allows an array of objects to be treated as a List. With Arrays.aslist, an array can be passed to any method that requires a Collection or a List. Foo[] result = Oldmethod (ARG);
Newmethod (arrays.aslist (result));
If the old API returns a Vector or Hashtable, you do not need to do a bit of work, because Vector has been transformed to implement the List interface, and Hashtable is also transformed to implement the MAP. A Vector can then be passed directly to any method that requires a Collection or a List.
Vector result = Oldmethod (ARG);
Newmethod (result);
Similarly, a Hashtable can be passed directly to any method that requires a Map.
Hashtable result = Oldmethod (ARG);
Newmethod (result);
More rarely, an API returns a enumeration that represents the object set of an object. Although there is no direct support for converting enumeration to Collection, it is also a simple matter to create a Collection that contains all the elements returned by enumeration:
Enumeration E = Oldmethod (ARG);
List L = new ArrayList ();
while (e smoreelements ())
L.add (E.nextelement ());
Newmethod (l);