Java 集合系列02之 Collection架构

概要

首先,我们对Collection进行说明。下面先看看Collection的一些框架类的关系图:

这里写图片描述

Collection 是一个接口,它主要的两个分支是:List 和 Set。

List和Set都是接口,它们继承于Collection。List是有序的队列,List中可以有重复的元素;而Set是数学概念中的集合,Set中没有重复元素!

List和Set都有它们各自的实现类。

为了方便,我们抽象出了 AbstractCollection 抽象类,它实现了 Collection 中的绝大部分函数;这样,在 Collection 的实现类中,我们就可以通过继承 AbstractCollection 省去重复编码。AbstractList 和 AbstractSet 都继承于 AbstractCollection,具体的 List 实现类继承于 AbstractList,而 Set 的实现类则继承于 AbstractSet。

另外,Collection 中有一个 iterator() 函数,它的作用是返回一个 Iterator 接口。通常,我们通过 Iterator 迭代器来遍历集合。ListIterator 是 List 接口所特有的,在 List 接口中,通过 ListIterator() 返回一个 ListIterator 对象。

接下来,我们看看各个接口和抽象类的介绍;然后,再对实现类进行详细的了解。

本章内容包括:

  1. Collection简介
  2. List简介
  3. Set简介
  4. AbstractCollection
  5. AbstractList
  6. AbstractSet
  7. Iterator
  8. ListIterator
1、Collection简介

Collection的定义如下:

public interface Collection<E> extends Iterable<E> {}

它是一个接口,是高度抽象出来的集合,它包含了集合的基本操作:添加、删除、清空、遍历(读取)、是否为空、获取大小、是否保护某元素等等。

Collection 接口的所有子类(直接子类和间接子类)都必须实现2种构造函数:不带参数的构造函数 和 参数为Collection的构造函数。带参数的构造函数,可以用来转换Collection的类型。

// Collection的API
abstract boolean         add(E object)
abstract boolean         addAll(Collection<? extends E> collection)
abstract void            clear()
abstract boolean         contains(Object object)
abstract boolean         containsAll(Collection<?> collection)
abstract boolean         equals(Object object)
abstract int             hashCode()
abstract boolean         isEmpty()
abstract Iterator<E>     iterator()
abstract boolean         remove(Object object)
abstract boolean         removeAll(Collection<?> collection)
abstract boolean         retainAll(Collection<?> collection)
abstract int             size()
abstract <T> T[]         toArray(T[] array)
abstract Object[]        toArray()
2、List简介

List的定义如下:

public interface List<E> extends Collection<E> {}

List 是一个继承于 Collection 的接口,即 List 是集合中的一种。List 是有序的队列,List 中的每一个元素都有一个索引;第一个元素的索引值是0,往后的元素的索引值依次+1。和Set不同,List中允许有重复的元素。

List的官方介绍如下:

A List is a collection which maintains an ordering for its elements. Every element in the List has an index. Each element can thus be accessed by its index, with the first index being zero. Normally, Lists allow duplicate elements, as compared to Sets, where elements have to be unique.

关于API方面。既然List是继承于 Collection 接口,它自然就包含了 Collection 中的全部函数接口;由于List是有序队列,它也额外的有自己的API接口。主要有“添加、删除、获取、修改指定位置的元素”、“获取List中的子队列”等。

// Collection的API
abstract boolean         add(E object)
abstract boolean         addAll(Collection<? extends E> collection)
abstract void            clear()
abstract boolean         contains(Object object)
abstract boolean         containsAll(Collection<?> collection)
abstract boolean         equals(Object object)
abstract int             hashCode()
abstract boolean         isEmpty()
abstract Iterator<E>     iterator()
abstract boolean         remove(Object object)
abstract boolean         removeAll(Collection<?> collection)
abstract boolean         retainAll(Collection<?> collection)
abstract int             size()
abstract <T> T[]         toArray(T[] array)
abstract Object[]        toArray()

// 相比与Collection,List新增的API:
abstract void                add(int location, E object)
abstract boolean             addAll(int location, Collection<? extends E> collection)
abstract E                   get(int location)
abstract int                 indexOf(Object object)
abstract int                 lastIndexOf(Object object)
abstract ListIterator<E>     listIterator(int location)
abstract ListIterator<E>     listIterator()
abstract E                   remove(int location)
abstract E                   set(int location, E object)
abstract List<E>             subList(int start, int end)
3、Set简介

Set的定义如下:

public interface Set<E> extends Collection<E> {}

Set 是一个继承于 Collection 的接口,即 Set 也是集合中的一种。Set 是没有重复元素的集合。

关于API方面。Set的API和Collection完全一样。

// Set的API
abstract boolean         add(E object)
abstract boolean         addAll(Collection<? extends E> collection)
abstract void             clear()
abstract boolean         contains(Object object)
abstract boolean         containsAll(Collection<?> collection)
abstract boolean         equals(Object object)
abstract int             hashCode()
abstract boolean         isEmpty()
abstract Iterator<E>     iterator()
abstract boolean         remove(Object object)
abstract boolean         removeAll(Collection<?> collection)
abstract boolean         retainAll(Collection<?> collection)
abstract int             size()
abstract <T> T[]         toArray(T[] array)
abstract Object[]         toArray()
4、AbstractCollection

AbstractCollection的定义如下:

public abstract class AbstractCollection<E> implements Collection<E> {}

AbstractCollection 是一个抽象类,它实现了 Collection 中除 iterator() 和 size() 之外的函数。

AbstractCollection 的主要作用:它实现了 Collection 接口中的大部分函数。从而方便其它类实现 Collection,比如ArrayList、LinkedList等,它们这些类想要实现 Collection 接口,通过继承 AbstractCollection 就已经实现了大部分的接口了。

5、AbstractList

AbstractList的定义如下:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {}

AbstractList 是一个继承于 AbstractCollection,并且实现 List 接口的抽象类。它实现了 List 中除size()、get(int location)之外的函数。

AbstractList 的主要作用:它实现了 List 接口中的大部分函数。从而方便其它类继承List。

另外,和 AbstractCollection 相比,AbstractList 抽象类中,实现了 iterator() 接口。

6、AbstractSet

AbstractSet的定义如下:

public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}

AbstractSet 是一个继承于 AbstractCollection,并且实现 Set 接口的抽象类。由于 Set 接口和 Collection 接口中的API完全一样,Set 也就没有自己单独的API。和 AbstractCollection 一样,它实现了List中除iterator()和size()之外的函数。

AbstractSet 的主要作用:它实现了Set接口中的大部分函数。从而方便其它类实现 Set 接口。

7、Iterator

Iterator 的定义如下:

public interface Iterator<E> {}

Iterator 是一个接口,它是集合的迭代器。集合可以通过 Iterator 去遍历集合中的元素。Iterator 提供的 API 接口,包括:是否存在下一个元素、获取下一个元素、删除当前元素。

注意:Iterator 遍历 Collection 时,是fail-fast机制的。即,当某一个线程A通过 iterator 去遍历某集合的过程中,若该集合的内容被其他线程所改变了;那么线程A访问集合时,就会抛出 ConcurrentModificationException 异常,产生 fail-fast 事件。关于 fail-fast 的详细内容,我们会在后面专门进行说明。TODO

// Iterator的API
abstract boolean hasNext()
abstract E next()
abstract void remove()
8、ListIterator

ListIterator的定义如下:

public interface ListIterator<E> extends Iterator<E> {}

ListIterator 是一个继承于 Iterator 的接口,它是队列迭代器。专门用于遍历 List,能提供向前/向后遍历。相比于 Iterator,它新增了添加、是否存在上一个元素、获取上一个元素等等API接口。

// ListIterator的API
// 继承于Iterator的接口
abstract boolean hasNext()
abstract E next()
abstract void remove()

// 新增API接口
abstract void add(E object)
abstract boolean hasPrevious()
abstract int nextIndex()
abstract E previous()
abstract int previousIndex()
abstract void set(E object)





来源:http://www.cnblogs.com/skywang12345/p/3308513.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值