The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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: Java集合框架旨在确保核心集合接口与Java平台早期版本中用于表示集合的类型(Vector
, Hashtable
, array, and Enumeration
. Vector
、Hashtable
、数组和Enumeration
)之间的完全互操作性。In this section, you'll learn how to transform old collections to the Java Collections Framework collections and vice versa.在本节中,您将学习如何将旧集合转换为Java集合框架集合,反之亦然。
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 假设旧API返回一个对象数组,而新API需要一个Collection
. Collection
。The Collections Framework has a convenience implementation that allows an array of objects to be viewed as a 集合框架有一个方便的实现,允许将对象数组视为List
. List
。You use 可以使用Arrays.asList
to pass an array to any method requiring a Collection
or a List
.Arrays.asList
将数组传递给任何需要Collection
或List
的方法。
Foo[] result = oldMethod(arg); newMethod(Arrays.asList(result));
If the old API returns a 如果旧的API返回一个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
. Vector
或Hashtable
,那么就没有什么工作要做了,因为Vector
被改装为实现List
接口,而Hashtable
被改装为实现Map
。Therefore, a 因此,Vector
may be passed directly to any method calling for a Collection
or a List
.Vector
可以直接传递给任何调用Collection
或List
的方法。
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 不太常见的情况是,API可能会返回表示对象集合的Enumeration
that represents a collection of objects. Enumeration
。The Collections.list
method translates an Enumeration
into a Collection
.Collections.list
方法将Enumeration
转换为Collection
。
Enumeration e = oldMethod(arg); newMethod(Collections.list(e));
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 假设新API返回一个Collection
, and the old API requires an array of Object
. 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 如果旧API需要字符串数组(或其他类型)而不是String
(or another type) instead of an array of Object
? 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 如果旧的API需要一个Vector
, the standard collection constructor comes in handy.Vector
,那么标准的集合构造函数就派上用场了。
Collection c = newMethod(); oldMethod(new Vector(c));
The case where the old API requires a 旧API需要Hashtable
is handled analogously.Hashtable
的情况类似地处理。
Map m = newMethod(); oldMethod(new Hashtable(m));
Finally, what do you do if the old API requires an 最后,如果旧API需要Enumeration
? 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));