原文链接:
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html
原文内容也一并附加在本文最后.
Java平台包含一个集合框架。
集合是表示一组对象的对象(如经典的Vector类)。
集合框架是用于表示和操作集合的统一体系结构,使集合可以独立于实现细节而被操纵。
集合框架的主要有点在于:
集合框架包括:
集合接口分为两大阵营,最基础的接口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.Map 不过他们并是真正的集合。
但是,这些接口包含集合视角的操作,这些操作可以将它们作为集合进行操作。Map有以下后代:
java.util.SortedMap java.util.NavigableMap java.util.concurrent.ConcurrentMap java.util.concurrent.ConcurrentNavigableMap
集合中的许多修改方法是可选的.
也就是允许实现类并不必须要实现一个或者多个这种操作.
如果你尝试使用,将会抛出运行时异常UnsupportedOperationException
每个实现的文档必须指定支持哪些可选操作。
引入了几个术语来帮助本规范:
一些实现可以做到限制哪些元素(或者在Map场景下的键和值)可以被存储。可能的限制包括要求元素:
如果尝试往有限制的接口实现中添加不符合的元素,会出现运行时异常,比如 ClassCastException, IllegalArgumentException, or a NullPointerException.
如果尝试在有限制的实现中移除一个不符合条件的元素或者测试是否存在,会导致异常
不过一些受限制的集合支持这种用法.
集合的实现类一般遵从这样子的形式 < 实现特性>加上<接口名称>的形式
下表中列出了通用实现
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 |
通用实现类支持集合框架中所有的可选操作,并且对于元素没有任何的限制
他们都是非同步的
不过Collections类中包含了很多静态的工厂方法-synchronization wrappers 同步包装器
可以在很多非同的集合中提供同步的功能.
所有新的实现都具有快速失败迭代器,它可以检测到非法的并发修改,并且快速而干净地(而不是报错异常)失败。
ps: fail-fast java集合框架中的一种机制,检测并发修改
AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList 和 AbstractMap
为了减少实现接口所需代价,提供了核心集合框架的基本实现。
这些类的API文档准确描述了每种方法的实现方式,
因此实现者知道哪些方法必须被重写,考虑到具体实现类的基本操作的性能。
应用程序中使用多个线程的集合的必须仔细编程,这个一般叫做并发编程
Java平台包含对并发编程的广泛支持。有关详细信息,请参阅Java Concurrency Utilities。
由于集合经常被使用,各种支持并发编程的接口和集合的实现都被包含在这些API中
这些类型,超出了前面提到过的同步包装,提供了并发编程中经常需要用到的特性
并发接口
BlockingQueue TransferQueue BlockingDeque ConcurrentMap ConcurrentNavigableMap
并发实现类
LinkedBlockingQueue ArrayBlockingQueue PriorityBlockingQueue DelayQueue SynchronousQueue LinkedBlockingDeque LinkedTransferQueue CopyOnWriteArrayList CopyOnWriteArraySet ConcurrentSkipListSet ConcurrentHashMap ConcurrentSkipListMap
主要的设计目标是编写一个体积小概念强大的API
新功能与当前程序差别不大,这一点非常重要.而且,需要在现有的基础上增加
而不是替代他们。
而且,新的API必须足够强大,才能够提供前面所述的所有优势。
为了使核心接口的数量保持较小,
接口不会尝试捕捉诸如易变性,可修改性和可调节性之类的细微区别
相反,在核心接口中一些调用是可选的
使得实现类可以抛出一个UnsupportedOperationException异常指示出他们不支持指定的可选操作
集合实现者必须清楚地在文档中表明实现支持哪些可选操作。
为了使每个核心接口中的方法数量保持较小,
只有在以下情况下,接口才包含方法:
这是一项真正的基本操作:就其他方面可以被合理定义而言, 它是一个基本的操作,
有一个不可抗拒的性能因素导致重要的实现类将会想要去重写它
所有关于集合的表现形式的互操作性至关重要。
这包括数组,在不改变语言的情况下,不能直接实现Collection接口。
因此,
该框架包含的方法可以将集合移入数组,将数组视为集合,将Map视为集合。
The Java platform includes a collections framework. A collection is an object that represents a group of objects (such as the classic Vector class). A collections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details.
The primary advantages of a collections framework are that it:
The collections framework consists of:
The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:
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:
Many of the modification methods in the collection interfaces are labeled 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:
Some implementations restrict what elements (or in the case of Maps, keys and values) can be stored. Possible restrictions include requiring elements to:
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 result in an exception. Some restricted collections permit this usage.
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).
The AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList 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, given the performance of the basic operations of a specific implementation.
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.
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.
These concurrent-aware interfaces are available:
The following concurrent-aware implementation classes are available. See the API documentation for the correct usage of these implementations.
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.
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.
To keep the number of methods in each core interface small, an interface contains a method only if either:
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.
Copyright © 1993, 2018, Oracle and/or its affiliates. All rights reserved.