The interoperability of Java APIs

Source: Internet
Author: User
Tags arrays new set requires

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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.