【JAVA集合框架一 】java集合框架官方介绍 Collections Framework Overview 集合框架总览 翻译 javase8 集合官方文档中文版
原文链接:
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html
原文内容也一并附加在本文最后.
简介:
Java平台包含一个集合框架。
集合是表示一组对象的对象(如经典的Vector类)。
集合框架是用于表示和操作集合的统一体系结构,使集合可以独立于实现细节而被操纵。
集合框架的主要有点在于:
- 通过提供数据结构和算法减少编程工作量,因此您不必亲自编写它们。
- 通过提供数据结构和算法的高性能实现来提高性能。由于每个接口的各种实现都是可以互换的,因此可以通过切换实现来调整程序。
- 通过建立通用语言来回传递集合,提供不相关的API之间的互操作性。--可以部分理解为向上转型 多态
- 通过学习多个特定的集合API来减少学习API所需的工作量。 --通过自顶而下的API设计风格
- 不要求您实现特定的集合API来减少设计和实现API所需的工作量。--通过提供抽象类提供了很多默认实现
- 通过为集合和算法提供标准接口来操纵它们,从而促进软件重用。
集合框架包括:
- 集合接口。表示不同类型的集合,例如Set,List和Map。这些接口构成了框架的基础。
- 通用实现。集合接口的主要实现。
- 遗留实现。早期版本Vector和Hashtable中的集合类进行了改进,以实现集合接口。
- 特殊用途的实现。设计用于特殊情况的实现。这些实现展示非标准的性能特征,使用限制或行为。
- 并发实现。为高并发使用而设计的实现。
- 包装实现。将功能(如同步)添加到其他实现。
- 更简化便利的实现. 集合接口的高性能低复杂实现.
- 抽象实现。集合接口的部分实现,以促进自定义实现。
- 算法。collections中非常有用的静态方法,比如例如排序列表。
- 基础架构。为集合即可欧提供重要支持的接口。--就是一些其他辅助的,比如Iterable
- 数组工具类。基本类型和引用对象数组的实用函数。严格来说,并不是集合框架的一部分,这个特性与集合框架同时添加到Java平台,并依赖于相同的基础结构。
集合接口:
集合接口分为两大阵营,最基础的接口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
每个实现的文档必须指定支持哪些可选操作。
引入了几个术语来帮助本规范:
- 不支持修改操作的集合(如添加,删除和清除)称为不可修改,非不可修改的集合是可修改的。;
- 如果集合额外保证集合对象中没有任何可见的修改那么称之为为不可变,非不可变的就是可变的;
- 保证集合大小不变的的集合,即使元素可以变化更改,称之为固定大小,非固定大小的列表称之为可变大小;
- 支持快速(通常是固定时间)索引元素访问的List是随机访问(RandomAccess) List,比如arrayList,不支持快速索引访问的称之为顺序访问 (sequential access)List ,RandomAccess标记接口指明一个list具有支持随机访问的特性,这一点保证了你可以编写通用的算法代码,通过选择不同的随机访问还是顺序访问List,以达到更好的性能.
一些实现可以做到限制哪些元素(或者在Map场景下的键和值)可以被存储。可能的限制包括要求元素:
- 特定类型
- 不能为null
- 遵从一些自定的断言判断式(Obey some arbitrary predicate)
如果尝试往有限制的接口实现中添加不符合的元素,会出现运行时异常,比如 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视为集合。
原文内容:
Collections Framework Overview
Introduction
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:
- 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. - Reduces the effort
required to learn APIs by requiring you to learn multiple ad hoc
collection APIs. - Reduces the effort
required to design and implement APIs by not requiring you to produce ad hoc
collections APIs. - 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. - 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. - 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. - 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.
Collection Interfaces
The collection interfaces are
divided into two groups. The most basic interface, java.util.Collection, has the
following descendants:
- 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
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.SortedMap
- java.util.NavigableMap
- java.util.concurrent.ConcurrentMap
- java.util.concurrent.ConcurrentNavigableMap
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:
- 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. - 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. - 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. - 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 known 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.
Some implementations restrict what elements (or in the
case of Maps,
keys and values) can be stored. Possible restrictions include requiring elements
to:
- Be of a particular type.
- Be not 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 result in an exception. Some restricted collections permit this
usage.
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).
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.
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.
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.
- 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.
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 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.
【JAVA集合框架一 】java集合框架官方介绍 Collections Framework Overview 集合框架总览 翻译 javase8 集合官方文档中文版的更多相关文章
- 2DToolkit官方文档中文版打地鼠教程(三):Sprite Collections 精灵集合
这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...
- Orleans 框架3.0 官方文档中文版系列一 —— 概述
关于这个翻译文档的一些说明: 之前逛博客园的时候,看见有个园友在自己的博客上介绍Orleans. 觉得Orleans 是个好东西. 当时心想:如果后面有业务需要的时候可以用用Orleans框架. 当真 ...
- NHibernate官方文档中文版-框架架构(Architecture)
总体概览 一个非常高层次的NHibernate架构: 这个图展示了NHibernate使用数据库和配置信息来为应用程序提供持久化服务(和持久化对象). 我们想展示一个更加详细的运行时架构.但是NHib ...
- 【Java架构:基础技术】一篇文章搞掂:Spring Boot 官方文档解读
本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文内容大部分是翻译和总结官方文档,可以到https://docs.spring.io/spring-boot/docs查看(此地 ...
- Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图
接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolv ...
- Spring 4 官方文档学习(十一)Web MVC 框架
介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...
- Spring 4 官方文档学习 Spring与Java EE技术的集成
本部分覆盖了以下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Ent ...
- Spring 4 官方文档学习(十三)集成其他web框架
重点是通用配置,非常建议看一下!有助于理解Spring的ApplicationContext与Servlet Container的关系! 1.介绍 Spring Web Flow SWF目标是成为we ...
- Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC
内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...
随机推荐
- 使用sort对数组中的对象的某个属性进行排序
var data=[ { code: "10004", grade: "5.6", planQuantity: 220, }, { code: "10 ...
- hbase常用操纵操作——增删改查
查询某个资金账户的信息 get 'dmp:hbase_tags','资金账号' 创建表 create 'emp', 'personal data', 'professional data' 在HBas ...
- 正则表达式校验yyyymmdd
正则表达式为 ([\\d]{4}(((0[13578]|1[02])((0[1-9])|([12][0-9])|(3[01])))|(((0[469])|11)((0[1-9])|([12][1-9] ...
- Apache启动不成功时,用命令行检测(新手)
1,在配置Apache服务器时,经常要在httpd.conf 修改和添加一些代码,编写中,误写或者写错时,无法正常启动时,直接报错The requested operation has failed! ...
- Windows + Apache + WSGI 部署Django
注意Python Apache和mod_wagi的版本要一致哦 1.安装Apache服务器(下载后,解压即可,目录不能有中文) 2.安装mod_wsgi (pip install 它的路径) 3.打开 ...
- Scala中的Implicit详解
Scala中的implicit关键字对于我们初学者像是一个谜一样的存在,一边惊讶于代码的简洁, 一边像在迷宫里打转一样地去找隐式的代码,因此我们团队结合目前的开发工作,将implicit作为一个专题进 ...
- Eclipse项目里面看源码和文档
Eclipse项目里面看源码 1.新建项目列表 2.进入struts2-core-2.3.20.jar,双击之后,看不到源码 3.右键struts2-core-2.3.20.jar,选择propert ...
- 在CentOS 7上安装和使用GlusterFS
GlusterFS aggregates various storage servers over Ethernet or Infiniband RDMA interconnect into one ...
- 关于c++的一篇随笔
众所周知c++是一门极其深奥的学科,正因为其深奥之处,才会让人们觉得学习起来特别难.当然,我想说我自己也不例外,想起当初就像一场噩梦一样,直到今日还历历在目.尽管如此,c++还是一门相当有魅力的课程, ...
- Nuget4.0 bug一粒
这个锅到底是nuget的还是msbuild的我也不是很确定 在使用Nuget4.0打包编译项目时 当执行到nuget pack %%~dpna.csproj -build -Prop Configur ...