Fork me on GitHub

【Java集合】Collection概述

Collection接口

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

框架:

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

List接口

1
public interface List<E> extends Collection<E>
  • 允许重复元素
  • 有序的队列,List中的每一个元素都有一个索引

Set接口

1
public interface Set<E> extends Collection<E>
  • 不允许重复元素

AbstractCollection

1
public abstract class AbstractCollection<E> implements Collection<E>
  • AbstractCollection是一个抽象类,它实现了Collection中除iterator()和size()之外的函数。
  • 要实现一个可变的集合,需要重写add()方法,iterator()需要重写remove方法
1
2
3
default void remove() {
throw new UnsupportedOperationException("remove"); //不支持操作
}

AbstractList

1
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
  • AbstractList是一个继承于AbstractCollection,并且实现List接口的抽象类。它实现了List中除size()、get(int location)之外的函数
  • 要实现一个可变List, 需要重写set(int, E);实现变长List, 需要重写add(int, E),remove(int)
  • 和AbstractCollection相比,AbstractList抽象类中,实现了iterator()接口
  • subList 子List, 子集合改变, List元素也跟着改变

    1
    2
    3
    4
    5
    class SubList<E> extends AbstractList<E> {
    private final AbstractList<E> l;
    private final int offset;
    private int size;
    }
  • equal()

直接”==”比较 => 是否List类型 => 遍历List,逐个比对是否一致

  • hashCode()
1
2
3
4
5
6
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}

AbstractSet

1
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E>
  • AbstractSet是一个继承于AbstractCollection,并且实现Set接口的抽象类
  • equal()
    直接”==”比较 => 是否Set类型 => 长度一致 => 遍历Set
  • hashCode() 每个元素的hash和

Iterator

1
public interface Iterator<E> {}
  • Iterator是一个接口,它是集合的迭代器。集合可以通过Iterator去遍历集合中的元素。
  • Iterator提供的API接口,包括:是否存在下一个元素、获取下一个元素、删除当前元素。
  • fail-fast机制 ConcurrentModificationException异常
  • Iterator 和 Enumeration 的比较:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Enumeration 
package java.util;

public interface Enumeration<E> {

boolean hasMoreElements();

E nextElement();
}

//Iterator
package java.util;

public interface Iterator<E> {
boolean hasNext();

E next();

void remove();
}

区别:

 1. Enumeration 是JDK 1.0添加的接口,使用到它的函数包括Vector、Hashtable等类 ;Iterator 是JDK 1.2才添加的接口,它也是为了HashMap、ArrayList等集合提供遍历接口 
    2. 函数接口不同,Enumeration不支持删除元素操作
3. Iterator支持**fail-fast**机制,而Enumeration不支持,所以Iterator遍历速度会比Enumeration慢
  • Iterator 和 Iterable 比较:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Iterable 接口
public interface Iterable<T> {

Iterator<T> iterator();

default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}

default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}

区别:

  1. Iterable 表示实现了这个接口的集合对象支持迭代 ,是可迭代的,能使用foreach遍历

    Iterator 是迭代器,提供迭代机制的对象,具体如何迭代,都是Iterator接口规范的

    1. Iterator 迭代出来的元素是原来集合元素的拷贝,指向对象的引用

ListIterator

1
public interface ListIterator<E> extends Iterator<E> {}
  • 提供向前/向后遍历
1
2
3
4
5
6
7
8
9
10
11
12
// 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