类
类修饰
abstract
继承
AbstractCollection
注:AbstractCollection源码分析可以看我的另一篇文章:https://blog.csdn.net/qq_43059674/article/details/88183974
实现
List
属性
protected transient int modCount = 0;
关于modCount 参考:https://blog.csdn.net/u012926924/article/details/50452411
方法
indexOf(Object o)
public int indexOf(Object o) {
//it 该对象的当前迭代对象
ListIterator<E> it = listIterator();
//o == null 防止空指针异常
if (o==null) {
while (it.hasNext())
//找到第一个为空的元素
if (it.next()==null)
//返回null在给集合中的位置
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
//没找的 return -1
return -1;
}
lastIndexOf(Object o)
public int lastIndexOf(Object o) {
//siez()指定从末尾开始迭代
ListIterator<E> it = listIterator(size());
//防止空指针异常
if (o==null) {
while (it.hasPrevious())
//从后往前遍历
if (it.previous()==null)
//返回随后调用 next()返回的元素的索
return it.nextIndex();
} else {
while (it.hasPrevious())
if (o.equals(it.previous()))
return it.nextIndex();
}
return -1;
}
addAll(int index, Collection<? extends E> c)
//从指定位置开始将c集合的元素添加进该集合对象
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
boolean modified = false;
//循环变了c集合添加进该集合
for (E e : c) {
add(index++, e);
modified = true;
}
return modified;
}
subList(int fromIndex, int toIndex)
public List<E> subList(int fromIndex, int toIndex) {
//判断 this 是否是instanceof 对象是否是这个特定类或者是它的子类的一个实例
//如果是返回 RandomAccessSubList<>(this, fromIndex, toIndex)
//不是返回 new SubList<>(this, fromIndex, toIndex))
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
详请参考:http://www.cnblogs.com/gaojing/archive/2012/06/17/java-list-sublist-caution.html
equals(Object o)
public boolean equals(Object o) {
//如果就是该对象本身直接返回true
if (o == this)
return true;
//不是List的实例直接返回 false
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();
/**
* 如果ol = null那么久返回 o2等不等于null
* 如果ol != null 使用 equals方法进行判断
* 如果有一个元素不想等就返回 false
*/
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
//看看是否都遍历完成
return !(e1.hasNext() || e2.hasNext());
}
hashCode()
public int hashCode() {
//首先 haqshCode 赋值为 1
int hashCode = 1;
//遍历该集合对象本身 所以元素共同参与
// 来得出该对象的hashCode值
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
removeRange(int fromIndex, int toIndex)
protected void removeRange(int fromIndex, int toIndex) {
//获得当前迭代对象,指定从 fromIndex 位开始
ListIterator<E> it = listIterator(fromIndex);
//循环圈数 toIndex - fromIndex 圈
for (int i=0, n=toIndex-fromIndex; i<n; i++) {
//不断的next()和移除元素
it.next();
it.remove();
}
}
rangeCheckForAdd(int index)
private void rangeCheckForAdd(int index) {
//如果index 小于0或者大于size抛异常
if (index < 0 || index > size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
subList(int fromIndex, int toIndex)
//构造一个SubList实现的List对象
public List<E> subList(int fromIndex, int toIndex) {
return new SubList<>(this, fromIndex, toIndex);
}
内部类
Itr
该集合的迭代器实现
private class Itr implements Iterator<E> {
//后续调用next返回的元素索引。
int cursor = 0;
//最近调用next或previous返回的元素索引。如果通过调用
// 删除此元素,则重置为-1。
int lastRet = -1;
//迭代器认为后备List应具有的modCount值。
// 如果违反了此期望,则迭代器*已检测到并发修改。
int expectedModCount = modCount;
//判断是否还有下一位元素啊
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
//获得下一位元素
E next = get(i);
//修改 最近调用next或previous返回的元素索引
lastRet = i;
//后续调用next返回的元素索引 + 1
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
//如果 lastRet小于0抛出异常
if (lastRet < 0)
throw new IllegalStateException();
//首先调用checkForComodification方法
checkForComodification();
try {
//调用该迭代器对于的集合对象的remove方法来移除该元素
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
//后续调用next返回的元素索引 因为删除往前移一位
cursor--;
//lastRet设置为 -1
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
//如果modCount 不等于expectedModCount抛异常
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
关于modCount 参考:https://blog.csdn.net/u012926924/article/details/50452411
ListIter
//继承了Itr 实现了List接口特有的ListIterator迭代器
private class ListItr extends Itr implements ListIterator<E> {
//构造方法指定 cursor
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();
}
}
//如果调用next要返回的元素索引
public int nextIndex() {
return cursor;
}
//如果调用previous要返回的索引
public int previousIndex() {
return cursor-1;
}
//修改当前位置的集合元素
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
//因为多线程所以调用
checkForComodification();
try {
//调用该迭代器对应的集合对象的set方法来修改当前元素
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;
//一定下次next返回的索引
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
SubList
//该内部类继承了 AbstractList
class SubList<E> extends AbstractList<E> {
//定义了下面三个属性
private final AbstractList<E> l;
private final int offset;
private int size;
SubList(AbstractList<E> list, int fromIndex, int toIndex) {
//fromIndex < 0 抛异常
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
//toIndex 大于list.size抛异常
if (toIndex > list.size())
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
//fromIndex 大于 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) {
//判断index是否越界
rangeCheck(index);
//这和modCount属性有关涉及线程,详请
// 参考:https://blog.csdn.net/qq_43059674/article/details/88183974
checkForComodification();
//其实是在l集合对象上做修改
return l.set(index+offset, element);
}
//获得指定位置的元素
public E get(int index) {
rangeCheck(index);
checkForComodification();
//也是在l集合上获取
return l.get(index+offset);
}
//返回当前的集合的元素数量
public int size() {
checkForComodification();
return size;
}
//把元素添加到指定位置
public void add(int index, E element) {
rangeCheckForAdd(index);
checkForComodification();
//嗯对还是在l集合上做修改
l.add(index+offset, element);
//更新当前对象的修改次数,和l集合对象同步
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) {
//注意没做 rangeCheck判断;
checkForComodification();
//调用l对象的rremoveRange的方法来删除
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);
//c的size
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
//其实操作的还是l集合对象
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 之前实现ListIterator
return new ListIterator<E>() {
//获取l对象的跌打 并指定位置即当前集合所在的开始位置
private final ListIterator<E> i = l.listIterator(index+offset);
//判断是否有下一位
public boolean hasNext() {
return nextIndex() < size;
}
//返回下一位元素 并移动当前位置
public E next() {
//判断是否有下一位 如果没有抛异常
if (hasNext())
//调用i迭代器获取下一位元素
return i.next();
else
throw new NoSuchElementException();
}
//判断是否还可以调用previous()方法 即:是否还可以向上移动
public boolean hasPrevious() {
return previousIndex() >= 0;
}
//返回列表中的上一个元素,并向后移动光标位置。f'h
public E previous() {
//先判断一下,是否还能向上移动
if (hasPrevious())
return i.previous();
else
throw new NoSuchElementException();
}
//返回列表中的上一个元素,并向后移动光标位置。
public int nextIndex() {
return i.nextIndex() - offset;
}
//返回由后续调用 previous()返回的元素的索引。
public int previousIndex() {
return i.previousIndex() - offset;
}
public void remove() {
i.remove();
//根新modCount值 由于内部类缘故所以
// SubList.this.modCount访问该迭代器所在的
// SubList 对象
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++;
}
};
}
//构造一个SubList 并将该对象作为参数传进去
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() {
//如果该对象的modCount属性和l的modCount属性
//不一样抛异常 这和该集合是非线程安全有关
if (this.modCount != l.modCount)
throw new ConcurrentModificationException();
}
}
RandomAccessSubList
把源码贴出来,比较简单
//继承自SubList实现了RandomAccess
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
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);
}
}