Web Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  Web Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Web Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Web Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Web Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
Web Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python Web Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
Web Hosting Web Hosting Sign-Up Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  Web Hosting Resellers web hosting, website hosting, web site hosting Web Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Web Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Web Hosting Welcome Web Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Web Hosting Fund Raising, Fundraising, web hosting Web Hosting Resellers, web Hosting Web Hosting Web Design, web Hosting Web Hosting Extra Services,  web Hosting Web Hosting Traffic Booster, web hosting Web Hosting Traffic Booster, web hosting Web Hosting Technical Support,  web Hosting Web Hosting webmaster tips,  web Hosting Web Hosting 30 Day Money Back, web hosting Web Hosting Legal Notices for Web Hosting Web Hosting Glossary Computer Terms for web Hosting Web Hosting Contact Information - web hosting

Site Map
Web Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP Web Hosting Web Hosting Web Hosting Web Hosting Web Hosting Compatibility (The Java™ Tutorials > Collections > Interoperability)
Trail: Collections
Lesson: Interoperability
Home Page > Collections > Interoperability
Compatibility
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, and Enumeration. In this section, you'll learn how to transform old collections to the Java Collections Framework collections and vice versa.

Upward Compatibility

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 a List. You use Arrays.asList to pass an array to any method requiring a Collection or a List.

Foo[] result = oldMethod(arg);
newMethod(Arrays.asList(result));
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.
Vector result = oldMethod(arg);
newMethod(result);
Similarly, a Hashtable may be passed directly to any method calling for a Map.
Hashtable 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.
Enumeration e = oldMethod(arg);
newMethod(Collections.list(e));

Backward Compatibility

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 of Object. As you're probably aware, the Collection interface contains a toArray method designed expressly for this situation.

Collection c = newMethod();
oldMethod(c.toArray());
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((String[]) c.toArray(new String[0]));
If the old API requires a Vector, the standard collection constructor comes in handy.
Collection c = newMethod();
oldMethod(new Vector(c));
The case where the old API requires a Hashtable is handled analogously.
Map m = newMethod();
oldMethod(new Hashtable(m));
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 is a static factory method that takes a Collection and returns an Enumeration over the elements of the Collection.
Collection c = newMethod();
oldMethod(Collections.enumeration(c));
Previous page: Interoperability
Next page: API Design
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting