AbstractList
此类提供了List接口的骨架实现,以最小化实现由"随机访问"数据存储(如阵列)支持的此接口所需的工作量。
构造函数
protected AbstractList() {
}
成员变量
1、protected transient int modCount = 0;
此列表已在结构上修改的次数。结构修改是那些改变列表大小或以其他方式扰乱它的方式,使得正在进行的迭代可能产生不正确的结果。
此字段由iterator和listIterator方法返回的迭代器和列表迭代器实现使用。
如果此字段的值意外更改,迭代器(或列表迭代器)将响应code,next,remove,previous,code set或code add操作。
这提供了fail-fast行为,而不是在迭代期间面临并发修改的非确定性行为。
方法
1、public boolean add(E e)
public boolean add(E e) {
add(size(), e);
return true;
}
将指定的元素追加到此列表的末尾,调用add(int,E)
2、abstract public E get(int index);
获取指定下标处的元素
3、public E set(int index, E element)
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
默认不支持set
4、public void add(int index, E element)
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
默认不支持add
5、public E remove(int index)
public E remove(int index) {
throw new UnsupportedOperationException();
}
默认不支持remove
6、public int indexOf(Object o)
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;
}
使用有序集合迭代器,如果找到了返回迭代器的previousIndex()
7、public int lastIndexOf(Object o)
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;
}
使用有序集合迭代器,从尾部开始,向前遍历,如果找到了返回迭代器的nextIndex()
8、public void clear()
public void clear() {
removeRange(0, size());
}
调用removeRange,范围是集合的开始到最后
9、public boolean addAll(int index, Collection<? extends E> c)
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;
}
先检查该下标能否插入,再使用增强for遍历c,自身不断调用add添加元素,如果有添加返回true
10、public Iterator iterator()
public Iterator<E> iterator() {
return new Itr();
}
返回一个单向迭代器
11、public ListIterator listIterator()
public ListIterator<E> listIterator() {
return listIterator(0);
}
返回一个双向迭代器,起始位置在第一个元素前
12、public ListIterator listIterator(final int index)
public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index);
return new ListItr(index);
}
先检查下标是否存在,返回一个双向迭代器,起始位置在指定下标元素前
13、public List subList(int fromIndex, int toIndex)
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
判断这个集合是否支持快速随机访问,以有无实现RandomAccess接口为标准,支持则返回RandomAccessSubList实例,否则返回SubList实例
14、public boolean equals(Object o)
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());
}
判断地址值,判断是否为List,使用各自迭代器遍历各元素进行比较,以及最后用hasNext判断大小是否一致
15、public int hashCode()
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
自身开始为1,每一个元素让自己*31,如果元素不为null,在加上元素的hashcode
*31提升性能,31 * i == (i << 5)- i, 现代的 VM 可以自动完成这种优化。
16、protected void removeRange(int fromIndex, int 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();
}
}
调用ListIterator,使用for循环,n存储删除的个数,遍历并删除
17、private void rangeCheckForAdd(int index)
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
判断传入的下标是否越界
18、private String outOfBoundsMsg(int index)
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size();
}
返回字符串包含下标和当前集合所含元素个数
内部类
Itr
Itr 实现了 Iterator接口
成员变量
1、int cursor = 0;
将通过后续调用返回到下一个的元素的索引
2、int lastRet = -1;
由最近一次调用next或previous返回的元素的索引。如果要删除的调用删除此元素,请重置为-1。
3、int expectedModCount = modCount;
获取当前List的操作次数
方法
1、public boolean hasNext()
public boolean hasNext() {
return cursor != size();
}
判断cursor是否和集合大小相同
2、public E next()
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();
}
}
先判断集合操作次数是否和自己的一致,一致则尝试调用get获取当前值,更新lastRet,cursor+1,返回元素,可能会越界抛出异常,因此先要用hasNext判断
3、public void remove()
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();
}
}
如果迭代器没有调用过Next,抛出异常。先判断集合操作次数是否和自己的一致,一致则调用集合的remove,如果迭代器当前下标比上一次操作的大,需要-1,重置lastRet,获取modCount
4、final void checkForComodification()
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
检查迭代器和集合操作次数是否一致,不一致抛出异常
ListItr
继承自Itr实现了ListIterator接口
构造方法
ListItr(int index) {
cursor = index;
}
指定了迭代器的起始位置
方法
1、public boolean hasPrevious()
public boolean hasPrevious() {
return cursor != 0;
}
通过判断cursor是否为0,判断前面有无元素
2、public E previous()
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();
}
}
先判断集合操作次数是否和自己的一致,一致则尝试调用get获取当前值,更新lastRet,cursor-1,返回元素,可能会越界抛出异常,因此先要用hasPrevious判断
3、public int nextIndex()
public int nextIndex() {
return cursor;
}
4、public int previousIndex()
public int previousIndex() {
return cursor-1;
}
5、public void set(E e)
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();
}
}
如果迭代器没有调用过Next或者Previous,抛出异常。先判断集合操作次数是否和自己的一致,一致则调用集合的set,获取modCount
6、public void add(E e)
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();
}
}
先判断集合操作次数是否和自己的一致,一致则调用集合的add,重置lastRet,获取modCount
外部类
SubList
继承AbstractList
构造函数
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;
}
成员变量
1、private final AbstractList l;
记录原List
2、private final int offset;
记录子List在原List的起始下标
3、private int size;
记录子List的大小
方法
大部分方法和AbstractList一致,只是起始位置为offset
public List<E> subList(int fromIndex, int toIndex) {
return new SubList<>(this, fromIndex, toIndex);
}
SubList的sublist只会是SubList
RandomAccessSubList
继承自SubList,实现了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);
}
RandomAccessSubList的sublist只会是RandomAccessSubList