A collection sometimes called a container is simply an object that groups multiple elements into a single unit. 集合有时称为容器它只是一个将多个元素组合成单个单元的对象。Collections are used to store, retrieve, manipulate, and communicate aggregate data. 集合用于存储、检索、操作和传递聚合数据。Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). 通常,它们表示形成自然组的数据项,例如扑克手(卡片集合)、邮件文件夹(信件集合)或电话簿(姓名到电话号码的映射)。If you have used the Java programming language or just about any other programming language you are already familiar with collections.如果您使用过Java编程语言或者几乎任何其他编程语言您肯定已经熟悉了集合。
What Is a Collections Framework?什么是集合框架?
A collections framework is a unified architecture for representing and manipulating collections. 集合框架是用于表示和操作集合的一致性体系结构。All collections frameworks contain the following:所有集合框架都包含以下内容:
Interfaces: These are abstract data types that represent collections. 接口:这些是表示集合的抽象数据类型。Interfaces allow collections to be manipulated independently of the details of their representation. 接口允许集合独立于其表示的细节进行操作。In object-oriented languages, interfaces generally form a hierarchy.在面向对象语言中,接口通常形成一个层次结构。
Implementations: These are the concrete implementations of the collection interfaces. 实现:这些是集合接口的具体实现。In essence, they are reusable data structures.本质上,它们是可重用的数据结构。
Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. 算法:这些方法对实现集合接口的对象执行有用的计算,如搜索和排序。The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. 这些算法被认为是多态的:也就是说,相同的方法可以用于相应集合接口的许多不同实现。In essence, algorithms are reusable functionality.本质上,算法是可重用的功能。
Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy. 除了java集合框架之外,集合框架最著名的例子是C++标准模板库(STL)和SimultActs的集合层次结构。Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. 从历史上看,集合框架相当复杂,这给了它们陡峭的学习曲线的声誉。We believe that the Java Collections Framework breaks with this tradition, as you will learn for yourself in this chapter.我们相信Java集合框架打破了这一传统,您将在本章中亲自学习。
Benefits of the Java Collections FrameworkJava集合框架的好处
The Java Collections Framework provides the following benefits:Java集合框架提供了以下好处:
Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level "plumbing" required to make it work. 减少编程工作量:通过提供有用的数据结构和算法,集合框架使您可以将精力集中在程序的重要部分,而不是使其工作所需的低级“管道”。By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.通过促进无关API之间的互操作性,Java Collections框架使您无需编写适配器对象或转换代码来连接API。
Increases program speed and quality: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. 提高程序速度和质量:此集合框架提供有用数据结构和算法的高性能、高质量实现。The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. 每个接口的各种实现都是可互换的,因此可以通过切换集合实现轻松地调整程序。Because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving programs' quality and performance.因为您不再需要编写自己的数据结构,所以您将有更多的时间致力于提高程序的质量和性能。
Allows interoperability among unrelated APIs: The collection interfaces are the vernacular by which APIs pass collections back and forth. 允许不相关API之间的互操作性:集合接口是API来回传递集合的通用语言。If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, our APIs will interoperate seamlessly, even though they were written independently.如果我的网络管理API提供了一组节点名称,并且您的GUI工具包需要一组列标题,那么我们的API将无缝地进行互操作,即使它们是独立编写的。
Reduces effort to learn and to use new APIs: Many APIs naturally take collections on input and furnish them as output. 减少学习和使用新API的工作量:许多API自然地将集合作为输入并作为输出提供。In the past, each such API had a small sub-API devoted to manipulating its collections. 在过去,每个这样的API都有一个小的子API,专门用于操作其集合。There was little consistency among these ad hoc collections sub-APIs, so you had to learn each one from scratch, and it was easy to make mistakes when using them. 这些临时集合子API之间几乎没有一致性,因此您必须从头开始学习每一个子API,并且在使用它们时很容易出错。With the advent of standard collection interfaces, the problem went away.随着标准收集接口的出现,问题消失了。
Reduces effort to design new APIs: This is the flip side of the previous advantage. 减少设计新API的工作量:这是以前优势的另一面。Designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces.设计者和实现者不必在每次创建依赖于集合的API时都重新发明轮子;相反,它们可以使用标准的集合接口。
Fosters software reuse: New data structures that conform to the standard collection interfaces are by nature reusable. 促进软件重用:符合标准收集接口的新数据结构本质上是可重用的。The same goes for new algorithms that operate on objects that implement these interfaces.对实现这些接口的对象进行操作的新算法也是如此。