AbstractList源码分析
AbstractList抽象类继承自AbstracCollection,实现了List接口,用于 给List提供了一个骨架实现
AbstractList中的参数
- 该参数表示该List被修改的次数,目的是为了控制并发访问。
protected transient int modCount = 0;
AbstractList中的方法
- 将指定的元素追加到此列表的末尾(该方法是进行数据的添加,添加的过程中 调用了add(index,e)方法,并且这个方法只是在函数体中抛出了异常,并不进行任何处理,而真正这个函数的功能将交由继承它的子类来进行实现)
public boolean add(E e) {
add(size(), e);
return true;
}
- 该类中的get方法是一个抽象方法
abstract public E get(int index);
- set方法
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
- remove方法
public E remove(int index) {
throw new UnsupportedOperationException();
}
- 此类可以用于查找list中是否存在元素o,(注意:list可以存在null,并且list中可以存储相同的数据),并返回其下标。
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;
}
- 调用列表迭代器,从后往前遍历,如果存在该元素,就返回该元素第一次出现的索引(即从前往后看的最后一次出现),否则返回-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;
}
- 调用void removeRange(int fromIndex, int toIndex),将0到列表长度范围内的元素全部删除,即清空
public void clear() {
removeRange(0, size());
}
- 此方法用于进行数据的赋值,赋值的对象为collection,首先此方法会调用rangeCheckForAdd(index)方法进行相关的下标检测,如果下标小于0或者是下边大于size的话,抛出outOfIndexException异常。最后进行相关的复制操作。
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;
}
- 如果该List实现了RandomAccess接口,返回一个新的RandomAccessSubList实例,
否则返回一个SubList实例,这两个类在后面定义。
//该方法作用是返回fromIndex到toIndex的集合
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
- list的相等条件:两者有相同的长度,并且所有位置上的元素都equal。
首先检查参数列表是否就是本列表,然后检查参数是否是列表类型,然后使用迭代器遍历两个列表,比较相同位置上的元素,以及长度是否相同
public boolean equals(Object o) {
//检查参数列表是否就是本列表
if (o == this)
return true;
//检查参数是否是该列表类型或其子类
if (!(o instanceof List))
return false;
ListIterator<E> e1 = listIterator();
ListIterator<?> e2 = ((List<?>) o).listIterator();
//迭代器遍历两个列表,比较相同位置上的元素是否相等
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
//判断长度是否相同
return !(e1.hasNext() || e2.hasNext());
}
- 获取哈希值
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
- 通过迭代器来删除指定的元素,而迭代器调用的是remove方法,所以这个方法的效率不高
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();
}
}
AbstractList中的内部类
- Itr(私有的内部类,实现了Iterator接口,正向遍历)
public Iterator<E> iterator() {
return new Itr();
}
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.
*/
//该变量作用为了检测并发情况下数据的修改,
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();
}
}
- ListItr(与9想法,逆向遍历,继承Itr,方法更多)
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();
}
}
}
- SubLIst和RandomAccessSubList(详情可自行观看源码):
SubList类继承AbstractList类,并且内部数据通过AbstractList进行存储。
下面该迭代器方法通过匿名内部类返回
public ListIterator<E> listIterator(final int index)
RandomAccessSubList就多了个RandomAccess接口,与SubList差别不大