Java 集合框架02---Collection的源碼分析
概要
Collection類圖
Collection 簡介
Collection 的定義
public interface Collection<E> extends Iterable<E>{}
Collection是一個(gè)接口,是高度抽象的集合,其包含了集合的基本操作,如添加,刪除,遍歷,是否為空,集合轉(zhuǎn)為數(shù)組,集合大小,集合之間的合并。
其API如下:
// 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) //是否包含某個(gè)集合
abstract boolean equals(Object object) //
abstract int hashCode() //獲取集合的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) //集合轉(zhuǎn)化成數(shù)組
abstract Object[] toArray() //集合轉(zhuǎn)化成數(shù)組
List 簡介
List的定義
public interface List<E> extends Collection<E>{}
List 是一個(gè)繼承于Collection的接口,即List是集合中的一種接口。所以,Collection 接口有的方法List中也有。
List 是一個(gè)有序隊(duì)列,集合的初始索引是0,以后每增加一個(gè)元素索引+1。List中允許有重復(fù)元素。
其增加的API如下:(與Collection相同的API不重復(fù)列出)
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) //獲取子集合
1
2
3
4
5
6
7
8
9
10
Set 簡介
Set的定義
public interface Set<E> extends Collection<E>{}
Set是一個(gè)繼承于Collection的接口,即List是集合中的一種接口。 不允許有重復(fù)元素。其API與Collection的API完全一樣。再次不再贅述。
Queue 簡介
AbstractCollection
AbstractCollection的定義是
public abstract class AbstractCollection<E> implements Collection<E>{}
AbstractCollection是一個(gè)實(shí)現(xiàn)了Collection的抽象類,其實(shí)現(xiàn)了Collection中除了iterator和size方法之外的方法。所以,繼承與AbstractCollection的其他實(shí)現(xiàn)類,只需要實(shí)現(xiàn)AbstractCollection類沒有實(shí)現(xiàn)的方法以及按照需求重寫相關(guān)方法。
public abstract Iterator<E> iterator();
public abstract int size();
1
2
具體分析下AbstractCollection類下的相關(guān)方法的實(shí)現(xiàn)。
public boolean isEmpty() {
return size() == 0;
}
//檢查集合是否包含特定元素
public boolean contains(Object o) {
Iterator<E> it = iterator();
if (o==null) { //任何非空集合都包含null
while (it.hasNext())
if (it.next()==null)
return true;
} else {
while (it.hasNext())
if (o.equals(it.next()))
return true;
}
return false;
}
//將集合轉(zhuǎn)化成數(shù)組
public Object[] toArray() {
// Estimate size of array; be prepared to see more or fewer elements
Object[] r = new Object[size()]; //定義一個(gè)與集合等大的數(shù)組
Iterator<E> it = iterator();
//循環(huán)將集合中的元素copy到數(shù)組r中
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) // fewer elements than expected
return Arrays.copyOf(r, i);
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
int i = r.length;
while (it.hasNext()) {
int cap = r.length;
if (i == cap) {
int newCap = cap + (cap >> 1) + 1;
// overflow-conscious code
if (newCap - MAX_ARRAY_SIZE > 0)
newCap = hugeCapacity(cap + 1);
r = Arrays.copyOf(r, newCap);
}
r[i++] = (T)it.next();
}
// trim if overallocated
return (i == r.length) ? r : Arrays.copyOf(r, i);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError
("Required array size too large");
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public <T> T[] toArray(T[] a) {
// Estimate size of array; be prepared to see more or fewer elements
int size = size();
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator<E> it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) { // fewer elements than expected
if (a == r) {
r[i] = null; // null-terminate
} else if (a.length < i) {
return Arrays.copyOf(r, i);
} else {
System.arraycopy(r, 0, a, 0, i);
if (a.length > i) {
a[i] = null;
}
}
return a;
}
r[i] = (T)it.next();
}
// more elements than expected
return it.hasNext() ? finishToArray(r, it) : r;
}
// Modification Operations
public boolean add(E e) {
throw new UnsupportedOperationException();
}
//刪除元素o
public boolean remove(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext()) {
if (it.next()==null) {
it.remove();
return true;
}
}
} else {
while (it.hasNext()) {
if (o.equals(it.next())) {
it.remove();
return true;
}
}
}
return false;
}
// Bulk Operations
public boolean containsAll(Collection<?> c) {
for (Object e : c)
if (!contains(e))
return false;
return true;
}
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
//刪除集合c中所有元素(如果存在的話)
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator<?> it = iterator();
while (it.hasNext()) {
if (c.contains(it.next())) {
it.remove();
modified = true;
}
}
return modified;
}
//刪除不在集合c中的所有元素(如果存在的話)
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator<E> it = iterator();
while (it.hasNext()) {
if (!c.contains(it.next())) {
it.remove();
modified = true;
}
}
return modified;
}
// 清空
public void clear() {
Iterator<E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
// String conversion
// 將List 轉(zhuǎn)成[String] biao'shi
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
AbstractList
AbstractList的定義
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>{}
從定義看出AbstractList繼承了AbstractCollection類。實(shí)現(xiàn)了List接口。與AbstractCollection類相比其實(shí)現(xiàn)了iterator方法。
AbstractList部分源碼分析
// Search Operations
//如果找到則返回該元素的前一個(gè)索引值,沒有就返回-1
public int indexOf(Object o) {
ListIterator<E> it = listIterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
//如果找到則返回該元素的后一個(gè)索引值,沒有就返回-1
public int lastIndexOf(Object o) {
ListIterator<E> it = listIterator(size());
if (o==null) {
while (it.hasPrevious())
if (it.previous()==null)
return it.nextIndex();
} else {
while (it.hasPrevious())
if (o.equals(it.previous()))
return it.nextIndex();
}
return -1;
}
// Bulk Operations
//清除集合中的元素
public void clear() {
removeRange(0, size());
}
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index); //檢查索引值是否合法
boolean modified = false;
for (E e : c) {
add(index++, e);
modified = true;
}
return modified;
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
protected void removeRange(int fromIndex, int toIndex) {
ListIterator<E> it = listIterator(fromIndex);
for (int i=0, n=toIndex-fromIndex; i<n; i++) {
it.next();
it.remove();
}
}
--------------------------------------------------------------------------------
public Iterator<E> iterator() {
return new Itr();
}
public ListIterator<E> listIterator() {
return listIterator(0);
}
public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index);
return new ListItr(index);
}
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.(源代碼的注釋)
* 譯:iterator 每次遍歷都會(huì)檢測modCount的值是否與List應(yīng)有的長度相等,如果不相等的話,則會(huì)拋出一個(gè)ConcurrentModificationException的異常
這種場景常見于多線程的情況下。當(dāng)一個(gè)線程正在遍歷集合list時(shí),如果在其中另外一個(gè)線程刪除了list中的某個(gè)元素,則會(huì)出現(xiàn)該異常
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public E previous() {
checkForComodification();
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;
return previous;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor-1;
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
--------------------------------------------------------------------------------
// 獲取List的子集subList,指定開始索引和結(jié)束索引
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;
private final int offset;
private int size;
/** 從`SubList`的構(gòu)造器我們可以看出,其并不是返回一個(gè)真正的子集,還是原有的集合List,只是偏移量改成了fromIndex,大小改成了toIndex - fromIndex,所以對子集增加,刪除元素會(huì)直接作用域原有的List*/
SubList(AbstractList<E> list, int fromIndex, int toIndex) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > list.size())
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
l = list;
offset = fromIndex;
size = toIndex - fromIndex;
this.modCount = l.modCount;
}
public E set(int index, E element) {
rangeCheck(index);
checkForComodification();
return l.set(index+offset, element);
}
public E get(int index) {
rangeCheck(index);
checkForComodification();
return l.get(index+offset);
}
public int size() {
checkForComodification();
return size;
}
public void add(int index, E element) {
rangeCheckForAdd(index);
checkForComodification();
l.add(index+offset, element);
this.modCount = l.modCount;
size++;
}
public E remove(int index) {
rangeCheck(index);
checkForComodification();
E result = l.remove(index+offset);
this.modCount = l.modCount;
size--;
return result;
}
protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
l.removeRange(fromIndex+offset, toIndex+offset);
this.modCount = l.modCount;
size -= (toIndex-fromIndex);
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
l.addAll(offset+index, c);
this.modCount = l.modCount;
size += cSize;
return true;
}
public Iterator<E> iterator() {
return listIterator();
}
public ListIterator<E> listIterator(final int index) {
checkForComodification();
rangeCheckForAdd(index);
return new ListIterator<E>() {
private final ListIterator<E> i = l.listIterator(index+offset);
public boolean hasNext() {
return nextIndex() < size;
}
public E next() {
if (hasNext())
return i.next();
else
throw new NoSuchElementException();
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public E previous() {
if (hasPrevious())
return i.previous();
else
throw new NoSuchElementException();
}
public int nextIndex() {
return i.nextIndex() - offset;
}
public int previousIndex() {
return i.previousIndex() - offset;
}
public void remove() {
i.remove();
SubList.this.modCount = l.modCount;
size--;
}
public void set(E e) {
i.set(e);
}
public void add(E e) {
i.add(e);
SubList.this.modCount = l.modCount;
size++;
}
};
}
public List<E> subList(int fromIndex, int toIndex) {
return new SubList<>(this, fromIndex, toIndex);
}
private void rangeCheck(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkForComodification() {
if (this.modCount != l.modCount)
throw new ConcurrentModificationException();
}
}
從上述源碼中,我理解的兩條重要信息是:
1. AbstractList是非線程安全的,當(dāng)一個(gè)線程在遍歷List時(shí),另外一個(gè)線程對該List進(jìn)行修改則會(huì)直接拋出ConcurrentModificationException的異常
2. 從SubList的構(gòu)造器我們可以看出,其并不是返回一個(gè)真正的子集,還是原有的集合List,只是偏移量改成了fromIndex,大小改成了toIndex - fromIndex,所以對子集增加,刪除元素會(huì)直接作用域原有的List
AbstractSet
AbstractSet的定義是
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E>{}
AbstractSet的部分源碼分析
protected AbstractSet() {
}
// Comparison and hashing
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Set))
return false;
Collection<?> c = (Collection<?>) o;
if (c.size() != size())
return false;
try {
return containsAll(c);
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
}
public int hashCode() {
int h = 0;
Iterator<E> i = iterator();
while (i.hasNext()) {
E obj = i.next();
if (obj != null)
h += obj.hashCode();
}
return h;
}
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
if (size() > c.size()) {
for (Iterator<?> i = c.iterator(); i.hasNext(); )
modified |= remove(i.next());
} else {
for (Iterator<?> i = iterator(); i.hasNext(); ) {
if (c.contains(i.next())) {
i.remove();
modified = true;
}
}
}
return modified;
}
從上述源碼中可以看出AbstractSet 只是重寫了AbstractCollection中的removeAll方法。
Iterator 簡介
Iterator的定義
public interface Iterator<E>{}
1
Iterator是一個(gè)接口,Iterator 是Collection分支下的集合的最重要的遍歷工具,所有的List和Set都需要依賴它。
從API中看出,其包含的方法有,是否有下一個(gè)元素,獲取下一個(gè)元素,刪除當(dāng)前元素。
Iterator遍歷集合時(shí)采取的是 fast-fail 機(jī)制,即,當(dāng)某一個(gè)線程A通過iterator去遍歷某集合的過程中,若該集合的內(nèi)容被其他線程所改變了,那么線程A訪問集合時(shí),就會(huì)拋出CurrentModificationException異常,產(chǎn)生fail-fast事件。
Iterator的API是
abstract boolean hasNext()
abstract E next()
abstract void remove()
ListIterator 簡介
ListIterator的定義
public interface ListIterator<E> extends Iterator<E> {}
ListIterator是一個(gè)繼承Iterator的接口,它是隊(duì)列迭代器。專門用于遍歷List,能提供向前和向后遍歷。相比于Iterator,它新增了添加、是否存在上一個(gè)元素、獲取上一個(gè)元素等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)
作者:碼農(nóng)飛哥
微信公眾號:碼農(nóng)飛哥