本章介绍 Java 集合框架。在这里,你将了解什么是collections 以及它们可以使你工作的更轻松,程序更优雅。你将了解核心元素——接口,实现类,聚合操作以及算法——这些元素构成了Java 集合框架。
简介(Introduction) 告诉你什么是集合,以及它们如何帮你减轻工作并让程序更优雅。你将了解构成集合框架的核心元素:接口,实现, 算法。
接口(Interfaces) 介绍 Java 集合框架的核心与灵魂——集合核心接口。你将了解通常如何高效使用这些接口,包括什么时候用什么接口。你还将了解每个接口的特点,这可以帮你快速确定你要用的接口。
聚合操作(Aggregate Operations) 为你遍历整个集合,使你更高效,更便捷的往集合中存放元素。
实现(Implementations) 详述 JDK 的 通用集合实现 以及告诉你什么时候用哪个实现。你也可以学习封装实现,它可以添加一些功能到通用实现中。
算法(Algorithms) 介绍 JDK 提供的用于操作集合的 多态算法(polymorphic algorithms) 。如果走运的话,你将不必再重写排序算法了!
自定义实现(Custom Implementations) 告诉你为什么你可能要写你自己的collection 实现(而不是使用 JDK 提供的通用实现),以及你怎样实现它。使用 JDK 的集合抽象实现 可以很容易实现自定义实现!
互操作性(Interoperability) 告诉你集合框架如何与旧 API 互操作。此外,还会告诉你如何设计新API,这样就可以与其他新API无缝的互操作。
集合简介
集合 —— 有时称为容器——仅仅一个对象,可以将一组元素放进一个单元中。集合用于存储,获取,操作以及沟通汇总数据。通常情况下,它们表示一个物质群体的数据项,例如一个扑克盒(卡片集),一个邮件夹(书信的集合),或者一个电话本(对应手机号和姓名的集合)。假如你已经用过Java编程语言,或者任意其他编程语言,那你肯定已经熟悉集合的概念了。
集合框架是什么?
集合框架 是一个代表和操作集合的统一架构。所有集合框架包含以下内容:
接口: 这些抽象数据类型代表集合。接口允许集合操作它们独立地内容。在面向对象语言中,接口通常形成层次。
实现: 这是集合接口的具体实现。在本质上,它们是可重用的数据结构。
算法: 这些运行有效的计算方法,例如搜索和排序,是实现集合接口的对象。这个算法叫做多态:是同一个方法可以被用在相同集合接口的多个不同实现上。在本质上,算法是可重用的功能。
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. 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 集合框架的益处
Java 集合框架提供了以下益处:
减少编程工作量: 通过提供有用的数据结构和算法,集合框架帮你专注于程序的重要部分,而不是程序必备的基础部分。By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.
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. 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.
Reduces effort to learn and to use new APIs: Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a small sub-API devoted to manipulating its collections. 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. 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. 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.
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.