Documentation

The Java™ Tutorials
Hide TOC
Compatibility兼容性
Trail: Collections
Lesson: 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. Java集合框架旨在确保核心集合接口与Java平台早期版本中用于表示集合的类型(VectorHashtable数组Enumeration)之间的完全互操作性。In this section, you'll learn how to transform old collections to the Java Collections Framework collections and vice versa.在本节中,您将学习如何将旧集合转换为Java集合框架集合,反之亦然。

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. 假设您使用的API与另一个需要对象实现集合接口的API同时返回遗留集合。To make the two APIs interoperate smoothly, you'll have to transform the legacy collections into modern collections. 为了使这两个API能够顺利地互操作,您必须将遗留集合转换为现代集合。Luckily, the Java Collections Framework makes this easy.幸运的是,Java集合框架使这变得很容易。

Suppose the old API returns an array of objects and the new API requires a Collection. 假设旧API返回一个对象数组,而新API需要一个CollectionThe Collections Framework has a convenience implementation that allows an array of objects to be viewed as a List. 集合框架有一个方便的实现,允许将对象数组视为ListYou use Arrays.asList to pass an array to any method requiring a Collection or a List.可以使用Arrays.asList将数组传递给任何需要CollectionList的方法。

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. 如果旧的API返回一个VectorHashtable,那么就没有什么工作要做了,因为Vector被改装为实现List接口,而Hashtable被改装为实现MapTherefore, a Vector may be passed directly to any method calling for a Collection or a List.因此,Vector可以直接传递给任何调用CollectionList的方法。

Vector result = oldMethod(arg);
newMethod(result);

Similarly, a Hashtable may be passed directly to any method calling for a Map.类似地,Hashtable可以直接传递给任何调用Map的方法。

Hashtable result = oldMethod(arg);
newMethod(result);

Less frequently, an API may return an Enumeration that represents a collection of objects. 不太常见的情况是,API可能会返回表示对象集合的EnumerationThe Collections.list method translates an Enumeration into a Collection.Collections.list方法将Enumeration转换为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. 假设您正在使用一个返回现代集合的API和另一个要求您传入遗留集合的API。To make the two APIs interoperate smoothly, you have to transform modern collections into old collections. 要使这两个API顺利互操作,必须将现代集合转换为旧集合。Again, the Java Collections Framework makes this easy.同样,Java 集合框架使这变得简单。

Suppose the new API returns a Collection, and the old API requires an array of Object. 假设新API返回一个Collection,而旧API需要一个Object数组。As you're probably aware, the Collection interface contains a toArray method designed expressly for this situation.正如您可能知道的,Collection接口包含一个专门为这种情况设计的toArray方法。

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? 如果旧API需要字符串数组(或其他类型)而不是Object数组,该怎么办?You just use the other form of toArray — the one that takes an array on input.你只需要使用另一种形式的toArray—在输入时接受数组的那个。

Collection c = newMethod();
oldMethod((String[]) c.toArray(new String[0]));

If the old API requires a Vector, the standard collection constructor comes in handy.如果旧的API需要一个Vector,那么标准的集合构造函数就派上用场了。

Collection c = newMethod();
oldMethod(new Vector(c));

The case where the old API requires a Hashtable is handled analogously.旧API需要Hashtable的情况类似地处理。

Map m = newMethod();
oldMethod(new Hashtable(m));

Finally, what do you do if the old API requires an Enumeration? 最后,如果旧API需要Enumeration,您会怎么做?This case isn't common, but it does happen from time to time, and the Collections.enumeration method was provided to handle it. 这种情况并不常见,但确实会时不时发生,我们提供了Collections.enumeration方法来处理它。This is a static factory method that takes a Collection and returns an Enumeration over the elements of the Collection.这是一个静态工厂方法,它接受Collection并返回Collection元素的Enumeration

Collection c = newMethod();
oldMethod(Collections.enumeration(c));

Previous page: Interoperability
Next page: API Design