【JAVA集合框架一 】java集合框架官方介绍 Collections Framework Overview 集合框架总览 翻译 javase8 集合官方文档中文版

时间:2022-04-27 05:20:40

原文链接:

https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html

原文内容也一并附加在本文最后.

简介:

Java平台包含一个集合框架。

集合是表示一组对象的对象(如经典的Vector类)。

集合框架是用于表示和操作集合的统一体系结构,使集合可以独立于实现细节而被操纵。

集合框架的主要有点在于:

  1. 通过提供数据结构和算法减少编程工作量,因此您不必亲自编写它们。
  2. 通过提供数据结构和算法的高性能实现来提高性能。由于每个接口的各种实现都是可以互换的,因此可以通过切换实现来调整程序。
  3. 通过建立通用语言来回传递集合,提供不相关的API之间的互操作性。--可以部分理解为向上转型 多态
  4. 通过学习多个特定的集合API来减少学习API所需的工作量。  --通过自顶而下的API设计风格
  5. 不要求您实现特定的集合API来减少设计和实现API所需的工作量。--通过提供抽象类提供了很多默认实现
  6. 通过为集合和算法提供标准接口来操纵它们,从而促进软件重用。

集合框架包括:

  1. 集合接口。表示不同类型的集合,例如Set,List和Map。这些接口构成了框架的基础。
  2. 通用实现。集合接口的主要实现。
  3. 遗留实现。早期版本Vector和Hashtable中的集合类进行了改进,以实现集合接口。
  4. 特殊用途的实现。设计用于特殊情况的实现。这些实现展示非标准的性能特征,使用限制或行为。
  5. 并发实现。为高并发使用而设计的实现。
  6. 包装实现。将功能(如同步)添加到其他实现。
  7. 更简化便利的实现.  集合接口的高性能低复杂实现.
  8. 抽象实现。集合接口的部分实现,以促进自定义实现。
  9. 算法。collections中非常有用的静态方法,比如例如排序列表。
  10. 基础架构。为集合即可欧提供重要支持的接口。--就是一些其他辅助的,比如Iterable
  11. 数组工具类。基本类型和引用对象数组的实用函数。严格来说,并不是集合框架的一部分,这个特性与集合框架同时添加到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

每个实现的文档必须指定支持哪些可选操作。

引入了几个术语来帮助本规范:

  1. 不支持修改操作的集合(如添加,删除和清除)称为不可修改,非不可修改的集合是可修改的。;
  2. 如果集合额外保证集合对象中没有任何可见的修改那么称之为为不可变,非不可变的就是可变的;
  3. 保证集合大小不变的的集合,即使元素可以变化更改,称之为固定大小,非固定大小的列表称之为可变大小;         
  4. 支持快速(通常是固定时间)索引元素访问的List是随机访问(RandomAccess) List,比如arrayList,不支持快速索引访问的称之为顺序访问 (sequential access)List ,RandomAccess标记接口指明一个list具有支持随机访问的特性,这一点保证了你可以编写通用的算法代码,通过选择不同的随机访问还是顺序访问List,以达到更好的性能.

          

一些实现可以做到限制哪些元素(或者在Map场景下的键和值)可以被存储。可能的限制包括要求元素:

  1. 特定类型
  2. 不能为null 
  3. 遵从一些自定的断言判断式(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
.
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:

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:

  • Collections that do not support modification
    operations (such as addremove 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 AbstractCollectionAbstractSetAbstractListAbstractSequentialList 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.


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.

Copyright © 1993, 2018, Oracle and/or its
affiliates. All rights reserved.