第一次仔细的看源码,我是直接将AbstractList的源码复制一份,放在自己建立的AbstractList上面,对英文注释去掉,改成中文注释,并且加入自己的理解.
package com.lin;
import java.util.*;
/**
* AbstractList 源码解析
*
* modCount 用来表示此列表被修改的次数
* Itr 用cursor 来表示当前的索引下标
* 用lastRet -1表示remove或者初始化的情况
* 正常表示的是当前元素的下标
* 用来做删除(remove)和修改(set)校验
* 用expectedModCount来保存进入Iter的modCount的值
* 用来做并发检验
*
* hashCode 遍历列表,hashCode值等于hashCode=31*hashCode+每个元素的hashcode
*
*
* @author LCW
* @since 2020/11/25 19:05
*/
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
/**
* modCount表示"此列表被修改的次数"。
* 结构性修改是指更改列表大小,或以其他方式干扰列表进行进度可能会产生不正确结果的方式。
*/
protected transient int modCount = 0;
/**
* 独家构造函数.(用于子类构造函数的调用,通常是隐式的.)
*/
protected AbstractList() {
}
/**
* 添加元素
*/
public boolean add(E e) {
add(size(), e);
return true;
}
/**
* 通过index查找元素
*/
abstract public E get(int index);
/**
* 此实现始终抛出UnsupportedOperationException,需要子类重写,直接调用会抛出异常
*/
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* 此实现始终抛出UnsupportedOperationException,需要子类重写,直接调用会抛出异常
*/
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* 此实现始终抛出UnsupportedOperationException,需要子类重写,直接调用会抛出异常
*/
public E remove(int index) {
throw new UnsupportedOperationException();
}
//搜索操作
/**
* 此实现首先获取一个列表迭代器
* 然后,迭代列表,直到找到指定的元素或到达列表的末尾。
*/
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;
}
/**
* 从列表最后往前查找元素
* 他的实现首先获得一个指向列表末尾的列表迭代器然后,向后迭代列表,直到找到指定的元素,或者到达列表的开头。
*/
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;
}
// 批量操作
/**
* 从此列表中删除所有元素。
*/
public void clear() {
removeRange(0, size());
}
/**
* 此实现在指定的集合上获取一个迭代器,并对其进行迭代,然后使用从迭代器获得的元素一次一次插入
* 到此列表的适当位置,为了提高效率,许多实现将覆盖此方法。
*/
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);//检查index是否合法,不合法抛出异常
boolean modified = false;
for (E e : c) {
add(index++, e);
modified = true;
}
return modified;
}
// 迭代器
/**
* 以正确的顺序返回此列表中元素的迭代器。
*/
public Iterator<E> iterator() {
return new Itr();
}
/**
* 返回ListIterator
*/
public ListIterator<E> listIterator() {
return listIterator(0);
}
/**
* 获取指定索引之后的ListIterator
*/
public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index);//检查index是否合法,不合法抛出异常
return new ListItr(index);//获取指定索引之后的ListIterator
}
private class Itr implements Iterator<E> {
/**
* 游标指向当前索引的下一个元素
*/
int cursor = 0;
/**
* lastRet=最近一次调用next()或previous()返回的元素的索引,
* (也就是说最近一次执行get(int index)方法的index)
* 如果调用remove删除元素,则重置为-1。
*/
int lastRet = -1;
/**
* 初始化一个数据等于modCount,如果在下面的执行中expectedModCount!=modCount,说明发生了并发修改
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
//获取元素
public E next() {
checkForComodification();//检测是否发生并发修改
try {
int i = cursor;
E next = get(i);
lastRet = i;//修改lastRet为当前索引
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();//检测是否发生并发修改
throw new NoSuchElementException();
}
}
//删除元素
public void remove() {
if (lastRet < 0)//说明没有获取当前元素,自然无法remove
throw new IllegalStateException();
checkForComodification();//检测是否发生并发修改
try {
//每进行一次List的修改,会修改modCount,一般是modCount++
AbstractList.this.remove(lastRet);//从List中移除该元素
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;//更新expectedModCount
} 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;//修改lastRet为当前索引
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();
}
}
}
/**
* 截取集合从fromIndex到toIndex的元素
*/
public List<E> subList(int fromIndex, int toIndex) {
/*
RandomAccess 是一个标志接口,表明实现这个这个接口的 List 集合是支持快速随机访问的。
也就是说,实现了这个接口的集合是支持 "快速随机访问" 策略的。
如果是实现了这个接口的 List,那么使用for循环的方式获取数据会优于用迭代器获取数据。
*/
return (this instanceof RandomAccess ? new RandomAccessSubList<>(this, fromIndex, toIndex)
: new SubList<>(this, fromIndex, toIndex));
}
// 比较和哈希
/**
* 比较指定对象与此列表是否相等。当且仅当指定对象也是一个列表,并且两个列表具有相同的大小,
* 并且两个列表中所有对应的元素对均相等时,返回true
*/
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 (!(Objects.equals(o1, 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;
}
/**
* 删除索引在 fromIndex和toIndex之间的所有元素。
*/
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();
}
}
/**
* 检查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();
}
}
class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;//用于保存整一个List,主要是为了减少遍历数组,也避免了创建新的内存空间给新的数组
private final int offset;
private int size;
/**
* 截取集合从fromIndex到toIndex的元素
*/
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;
}
//将索引为index的元素设置为element
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) {
rangeCheck(index);//检查元素是否合法
checkForComodification();//检查是否发生并发情况
l.add(index + offset, element);//交给父类去实现
this.modCount = l.modCount;//更新modCount
size++;
}
/**
* 移除指定索引的数据
*/
public E remove(int index) {
rangeCheck(index);//检查元素是否合法
checkForComodification();//检查是否发生并发情况
E result = l.remove(index + offset);//移除数据
this.modCount = l.modCount;//更新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);
}
/**
* 往集合中指定索引添加一个列表,添加成功返回true,否则返回false
*/
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);//获取分割后的List的迭代器
//判断是否存在下一个元素
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);
}
//在当前元素后面添加元素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);
}
/**
* 检查索引index是否合法
*/
private void rangeCheck(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* 检查索引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();
}
}
//截取List的数据
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
//都是调用subList的方法进行截取
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
super(list, fromIndex, toIndex);
}
public List<E> subList(int fromIndex, int toIndex) {
return new RandomAccessSubList<>(this, fromIndex, toIndex);
}
}