- 原在线文档地址:http://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html
- 翻译时间:20150218
- 翻译:启明星辰 www.fuqichen.com
Collections Framework Overview (集合框架概览)
Introduction (简介)
The Java platform includes a collections framework.
Java平台中有一个集合框架.
A collection is an object that represents a group of objects (such as the classic Vector class).
一个集合(collection)是一个对象, 用来代表一群对象.(例如经典的 Vector 类就是集合的一种)
A collections framework is a unified architecture for representing and manipulating collections, enabling collection to be manipulated independently of implementation details.
集合框架是代表与操作集合的统一架构, 允许集合的实现细节被独立操作.
The primary advantages of a collections framework are that it:
集合框架的主要优点有:
-
Reduces programming effort by providing data structures and algorithms so you don’t have to write them yourself.
减少代码量, 通过所提供的数据结构与算法, 你无须自己去写这些.
-
Increases performance by providing high-performance implementations of data structures and algorithms. Because the various implementations of each interface are interchangeable, programs can be tuned by switching implementations.
提高性能, 通过所提供的高性能数据结构与算法. 因为每个接口的多种实现是可替换的, 所以可以通过替换不同实现来使程序更加协调.
-
Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.
提供不相关API间的交互操作性, 通过建立一种来回传递集合的通用语言.
-
Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc collection APIs.
减少学习新API所需时间, 通过要求你学习数个特设的集合API.
-
Reduces the effort required to design and implement APIs by not requiring you to produce ad hoc collections APIs.
减少设计与实现API所需时间, 你无须去写特设集合API.
-
Fosters software reuse by providing a standard interface for collections and algorithms with which to manipulate them.
提高软件复用性, 通过提供标准集合接口与算法去操作集合.
The collections framework consists of:
集合框架由以下部分组成:
-
Collection interfaces. Represent different types of collections, such as sets, lists, and maps. These interfaces form the basis of the framework.
集合接口. 代表不同类型的集合, 比如 Set, List, Map. 这些接口形成了集合框架的基础.
-
General-purpose implementations. Primary implementations of the collection interfaces.
通用目的实现类. 集合接口的主要实现类.
-
Legacy implementations. The collection classes from earlier releases, Vector and Hashtable, were retrofitted to implement the collection interfaces.
遗留实现类. 来自先前发行版本的集合类, 比如 Vector 与 Hashtable, 翻新后也实现了集合接口.
-
Special-purpose implementations. Implementations designed for use in special situations. These implementations display nonstandard performance characteristics, usage restrictions, or behavior.
特殊目的实现类. 为在特殊环境下使用而设计的实现类. 这些实现类有不标准的性能特点, 有使用限制, 或者有特殊行为.
-
Concurrent implementations. Implementations designed for highly concurrent use.
并发实现类. 为在高并发情况下使用而设计的实现类.
-
Wrapper implementations. Add functionality, such as synchronization, to other implementations.
包装实现类. 增加功能性, 比如与其他实现类同步.
-
Convenience implementations. High-performance “mini-implementations” of the collection interfaces.
便捷实现类. 集合接口的高性能迷你版实现类.
-
Abstract implementations. Partial implementations of the collection interfaces to facilitate custom implementations.
抽象实现类. 集合接口的部分实现,为使定制化实现更容易.
-
Algorithms. Static methods that perform useful functions on collections, such as sorting a list.
算法. 一些在集合上执行有用功能的静态方法, 例如给一个列表(List)排序.
-
Infrastructure. Interfaces that provide essential support for the collection interfaces.
基础架构. 为集合接口提供必要支持的接口.
-
Array Utilities. Utility functions for arrays of primitive types and reference objects. Not, strictly speaking, a part of the collections framework, this feature was added to the Java platform at the same time as the collections framework and relies on some of the same infrastructure.
数组工具. 用于由原生类型或引用对象组成的数组的实用函数. 严格来说, 这并不是集合框架的一部分, 这个特性在集合框架加入Java平台的同时加入, 并且依赖一些相同的基础架构.
Collection Interfaces (集合接口)
The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:
集合接口被划分为2部分. java.util.Collection 是最基本的一个, 它有以下衍生接口:
- java.util.Set
- java.util.SortedSet
- java.util.NavigableSet
- java.util.Queue
- java.util.concurrent.BlockingQueue
- java.util.concurrent.TransferQueue
- java.util.Deque
- java.util.concurrent.BlockingDeque
- java.util.List (这一个原文中没写,译者加入)
The other collection interfaces are based on java.util.Map and are not true collections. However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Map has the following offspring:
其他集合接口则基于 java.util.Map, 这些其实并不是真正的集合. 然而, 由于这些接口包含集合视角的操作方法, 所以允许将它们当作集合来操作. Map接口有以下子接口:
- java.util.SortedMap
- java.util.NavigableMap
- java.util.concurrent.ConcurrentMap
- java.util.concurrent.ConcurrentNavigableMap
Many of the modification methods in the collection interfaces are labled optional. Implementations are permitted to not perform one or more of these operations, throwing a runtime exception (UnsupportedOperationException) if they are attempted. The documentation for each implementation must specify which optional operations are supported. Several terms are introduced to aid in this specification:
集合接口中许多修改集合的方法被标明是可选的. 实现类被允许不去履行这些方法, 如果尝试的话会抛出 UnsupportedOperationException 运行时异常. 每个实现类的文档必须明确指出支持哪些可选操作. 以下详述介绍了数种说法去辅助理解:
- Collections that do not support modification operations (such as add, remove, and clear) are referred to as unmodifiable. Collections that are not unmodifiable are modifiable.
不支持修改操作(比如: add, remove, clear)的集合被称为 unmodifiable(不可修改的). 不是 unmodifiable 的集合则是 modifiable(可修改的).
- Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable. Collections that are not immutable are mutable.
额外保证集合对象不会改变的集合被称为 immutable(不可变的). 不是 immutable 的集合则是 mutable(可变的).
- Lists that guarantee that their size remains constant even though the elements can change are referred to as fixed-size. Lists that are not fixed-size are referred to as variable-size.
保证长度(元素个数)不变(即使元素可以变)的列表(List)被称为 fixed-size(固定长). 不是 fixed-size 的列表则是 variable-size(可变长度).
- Lists that support fast (generally constant time) indexed element access are known as random access lists. Lists that do not support fast indexed element access are kmown as sequential access lists. The RandomAccess marker interface enables lists to advertise the fact that they support random access. This enables generic algorithms to change their behavior to provide good performance when applied to either random or sequential access lists.
支持快速(通常是固定时间)索引元素访问的列表(List)被称为 random access list (随机访问列表). 不支持的则被称为 sequential access list (顺序访问列表). 实现 RandomAccess 标记接口的列表相当于声明其支持随机访问. 允许通用算法更改其行为, 从而当其应用于随机或顺出访问列表时, 都可以提供良好性能.
Some implementations restrict what elements (or in the case of Maps, keys, and values) can be stored. Possible restrictions include requiring elements to:
一些实现类限制只能存储特定的元素(或者在Map,key,value的情况下). 对元素可能的限制有:
- Be of a particular type. 是特定类型.
- Be not null. 不是 null.
- Obey some arbitrary predicate. 遵守一些自定的断言.
Attempting to add an element that violates an implementation’s restrictions results in a runtime exception, typically a ClassCastException, an IllegalArgumentException, or a NullPointerException. Attempting to remove or test for the presence of an element that violates an implementation’s restrictions can results in an exception. Some restricted collections permit this usage.
尝试添加一个违反某实现类限制的元素, 会导致运行时异常, 通常是 ClassCastException(类型转换异常), IllegalArgumentException(非法参数异常), 或者 NullPointerException(空指针异常). 尝试删除或者查询一个违反某实现类限制的元素, 也会导致异常. 但是也有一些有限制的集合允许这样做.
Collection Implementations (集合实现类)
Classes that implement the collection interfaces typically have names in the form of [Implementation-style - Interface]. The general purpose implementations are summarized in the following table:
典型的集合接口实现类通常有这样形式的名字 <实现类风格><接口名>. 以下表格是集合通用目的实现类的概览:
Interface | Hash Table | Resizable Array | Balanced Tree | Linked List | Hash Table + Linked List |
---|---|---|---|---|---|
Set | HashSet | TreeSet | LinkedHashSet | ||
List | ArrayList | LinkedList | |||
Deque | ArrayDeque | LinkedList | |||
Map | HashMap | TreeMap | LinkedHashMap |
The general-purpose implementations support all of the optional operations in the collection interfaces and have no restrictions on the elements they may contain. They are unsynchronized, but the Collections class contains static factories called synchronization wrappers that can be used to add synchronization to many unsynchronized collections. All of the new implementations have fail-fast iterators, which detect invalid concurrent modification, and fail quickly and cleanly (rather than behaving erratically).
这些通用目的实现类在集合接口上支持所有的可选操作,对于所包含的元素也没有限制. 他们是不同步的, 但是如果集合类包含静态元素则被称为synchronization wrappers (同步包装器), 可以用来将同步功能添加到很多不同步集合中. 所有的新实现类都有 fail-fast iterators (快速舍弃迭代器), 用以探测无效并发修改, 以及快速彻底地舍弃(而不是不规律的行为).
The AbstractCollection, AbstractSet, AbstractList, AbstrsctSequentialList and AbstractMap classes provide basic implementations of the core collection interfaces, to minimize the effort required to implement them. The API documentation for these classes describes precisely how each method is implemented so the implementer knows which methods must be overridden, give the performance of the basic operations of a specific implementation.
AbstractCollection, AbstractSet, AbstractList, AbstrsctSequentialList 还有 AbstractMap, 这些抽象类提供了核心接口 Collection 的基本实现, 减少了实现他们所需的时间. 这些类的 API 文档精确描述了每一个方法是如何实现的, 以使实现者知道哪些方法必须被重写, API 文档还给出了每种特定实现的基本操作的所需表现.
Concurrent Collections (并发集合)
Applications that use collections from more than one thread must be carefully programmed. In general, this is known as concurrent programming. The Java platform includes extensive support for concurrent programming. See Java Concurrency Utilities for details.
应用程序在多线程上使用集合时必须要小心编程.通常,这被称为concurrent programming(并发编程). Java平台包含有对并发编程所提供的扩展支持. 详细请见 Java Concurrency Utilities.
Collections are so frequently used that various concurrent friendly interfaces and implementations of collections are included in the APIs. These types go beyond the synchronization wrappers discussed previously to provide features that are frequently needed in concurrent programming.
集合是如此被频繁的使用,以至于繁多的并发友好接口与集合实现类被包含于API中. 这些类型超过了之前提到的同步包装器, 需要提供在并发编程中频繁需要的一些特性.
These concurrent-aware interfaces are available:
有如下支持并发的接口可使用:
The follwing concurrent-aware implementation classes are available. See the API documentation for the correct usage of these implementations.
有如下支持并发的实现类可使用. 获知这些实现类的准确用法请查看 API 文档.
- LinkedBlockingQueue
- ArrayBlockingQueue
- PriorityBlockingQueue
- DelayQueue
- SynchronousQueue
- LinkedBlockingDeque
- LinkedTransferQueue
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- ConcurrentSkipListSet
- ConcurrentHashMap
- ConcurrentSkipListMap
Design Goals (设计目标)
The main design goal was to produce an API that was small in size and, more importantly, in “conceptual weight”. It was critical that the new functionality not seem too different to current Java programmers; it had to augment current facilities, rather than replace them. At the same time, the new API had to be powerful enough to provide all the advantages described previously.
主要的设计目标是产生一个 API, 它小尺寸,更重要的是,重概念. 至关重要的一点是新的功能与现有的 Java 程序看起来并无不同; 必须扩大现有设施, 而不是替代它们. 同时,新 API 必须足够强大来提供如前所描述的所有优点.
To keep the number of core interfaces small, the interfaces do not attempt to capture such subtle distinctions as mutability, modifiability, and resizability. Instead, certain calls in the core interfaces are optional, enabling implementations to throw an UnsupportedOperationException to indicate that they do not support a specified optional operation. Collection implementers must clearly document which optional operations are supported by an implementation.
为使核心接口的数量足够少, 这些接口并不试图捕捉一些细微差别,比如 mutability(集合对象可变性), modifiability(集合元素可修改性), 以及 resizability(集合长度可变性). 相反, 在核心接口中这些明确的要求是可选的, 允许实现类抛出 UnsupportedOperationException(不支持操作异常) 去表明它们不支持某个特定的可选操作. 集合实现类必须在文档中清楚指出每个实现类都支持哪些操作.
To keep the number of methods in each core interfaces small, an interface contains a method only if either:
为使每个核心接口中方法的数量足够少, 一个接口若要包含某个方法必须符合如下条件之一:
- It is a truly fundamental operation: a basic operations in terms of which others could be reasonably defined.
它是一个真正重要而基础的操作: 就其他方面可以被合理定义而言, 它是一个基本的操作.
- There is a compelling performance reason why an important implementation would want to override it.
有一个不可抗拒的性能因素导致重要的实现类将会想要去重写它.
It was critical that all reasonable representations of collections interoperate well. This included arrays, which cannot be made to implement the Collection interface directly without changing the language. Thus, the framework includes methods to enable collections to be moved into arrays, arrays to be viewed as collections, and maps to be viewed as collections.
至关重要的一点是所有合理的集合代表都可以顺畅地进行交互操作. 这包括数组, 数组不能被用来直接去实现集合接口, 除非改变语言. 因此, 集合框架包含了可以将集合转变为数组的方法, 以及将数组与 Map(键值对集合) 视为集合查看的方法.
- 启明星辰 www.fuqichen.com