概述
下图是Collection框架的类关系图:
Collection是一个接口,主要有2个分支,分别是List和Set。List和Set都是接口,他们都继承自Collection接口。List是有序队列,可以有重复元素;Set是数学概念中的集合,无序的,也是没有重复元素的(数学上的集合有三个特征:无序性、互异性、确定性)。
为了方便,JDK抽象出了AbstractCollection抽象类,它实现了Collection接口中大部分方法。这样在Collection的实现类中,就可以通过集成AbstractCollection省去了很多重复的编码工作。AbstractList和AbstractSet都继承自AbstractCollection,具体的List实现类继承自AbstractList(例如ArrayList、LinkedList、Vector、Stack等),而具体的Set实现类继承自AbstractSet(例如HashSet、TreeSet等)。
另外,Collection还继承Iterable接口,里面有一个iterator()方法,它返回Iterator接口。通常,我们可以通过Iterator迭代器来遍历集合。ListLiterator接口是List接口特有的,在List接口中,通过调用listIterator()返回一个ListIterator迭代器对象。
Collection接口
Collection的接口定义如下:
|
|
它是一个接口,是高度抽象出来的(OOP的四大特征之一)。它包含了集合的基本操作:添加、删除、遍历(读取)、修改、清空、获取大小、是否为空、是否保护某元素等。
Collection接口的所有子类(直接子类或间接子类)都应该提供2种标准的构造器:不带参数和带Collection参数。不带参数的构造器用来生成一个空集合,带Collection参数的构造器用来生成拥有一样元素的集合。JDK文档中是这样描述的:
All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two “standard” constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument.
Collection的API如下:
List接口
List接口的定义如下:
List是一个继承于Collection
的接口,既List是Collection的一种。List是有序的队列,List中的每一个元素都有索引,第一个的索引值是0,往后依次加1。和Set不同,List中允许重复的元素。官方文档如下:
List的API如下:
Set接口
Set的定义如下:
Set也是Collection上一种。它没有重复的元素,是数学概念上的集合。官方文档解释如下:
Set的API如下:
AbstractCollection抽象类
AbstractCollection的定义如下:
AbstractCollection是一个抽象类,它实现了Collection接口中除了iterator()
和size()
之外的方法。它的主要作用是:实现了Collection接口中的大部分接口方法,从而方便其他类,比如ArrayList、HashSet等,他们这些类想要实现Collection接口,通过继承AbstractCollection就已经实现了大部分的接口方法了。但是注意AbstractCollection里面有个方法是是假实现,add()
,它直接就抛出一个UnsupportedOperationException
异常,并没有真正的实现。
AbstractList抽象类
AbstractList的定义如下:
AbstractList是一个抽象类,它实现了List中除get()
、size()
之外的接口方法,但是它的remove()
, add()
, set()
都是假实现。 对比AbstractCollection,AbstractList实现了iterator()
。
AbstractSet抽象类
AbstractSet的定义如下:
AbstractSet是一个抽象类,由于Set的API完全和Collection一样,所以AbstractSet和AbstractCollection一样,没有实现iterator()
、size()
,但是它细化了AbstractCollection的很多实现。作用也是方便其他类实现Set接口。
Iterator
Iterator的定义如下:
Iterator是一个迭代器,用来遍历集合中的所有元素。
Iterator的API如下:
注意,Iterator遍历集合的时候,是fast-fail机制
的。即,当线程A通过iterator去遍历集合的时候,如果该集合被线程B改变了,那么当线程A访问集合的时候,就会抛出ConcurrentModificationException
异常,产生fast-fail事件。关于fast-fail我们后面再详细讨论。
ListIterator
ListIterator的定义如下:
ListIterator是继承于Iterator接口的,它是一个队列迭代器,专门服务于List,能提供向前/向后遍历。相比于Iterator,增加了一些接口。官方文档描述如下:
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
ListIterator的API如下:
Iterable
Iterable的定义如下:
Iterable是一个接口,实现了这个接口的类都可以使用for-each loop
语句来遍历,比如Collection继承了Iterable,所以所有的Collection都是可以使用for-each loop
语句来遍历的。
Iterable的API如下: