Java SE 是什么,包括哪些内容(二十一)?
本文内容参考自Java8标准
有序集合类(列表)承上启下的顺序访问抽象类:
AbstractSequentialList
参看随机访问抽象类:AbstractList
[https://blog.youkuaiyun.com/ruidianbaihuo/article/details/103449793]
其中加粗表示提供了实现,–表示当前接口/类中不存在这个方法。
List | AbstractCollection | AbstractSequentialList |
---|---|---|
size() | size() | – |
isEmpty() | isEmpty() | – |
contains(Object o) | contains(Object o) | – |
iterator() | iterator() | iterator() |
toArray() | toArray() | – |
toArray(T[] a) | toArray(T[] a) | – |
– | finishToArray(T[] r, Iterator<?> it) | – |
– | hugeCapacity(int minCapacity) | – |
add(E e) | add(E e) | – |
remove(Object o) | remove(Object o) | – |
containsAll(Collection<?> c) | containsAll(Collection<?> c) | – |
addAll(Collection<? extends E> c) | addAll(Collection<? extends E> c) | – |
addAll(int index, Collection<? extends E> c) | – | addAll(int index, Collection<? extends E> c) |
removeAll(Collection<?> c) | removeAll(Collection<?> c) | – |
retainAll(Collection<?> c) | retainAll(Collection<?> c) | – |
replaceAll(UnaryOperator< E > operator) | – | – |
sort(Comparator<? super E> c) | – | – |
clear() | clear() | – |
equals(Object o) | – | – |
hashCode() | – | – |
get(int index) | – | get(int index) |
set(int index, E element) | – | set(int index, E element) |
add(int index, E element) | – | add(int index, E element) |
remove(int index) | – | remove(int index) |
indexOf(Object o) | – | – |
lastIndexOf(Object o) | – | – |
listIterator() | – | – |
listIterator(int index) | – | listIterator(int index) |
subList(int fromIndex, int toIndex) | – | – |
spliterator() | – | – |
– | toString() | – |
类声明:
public abstract class AbstractSequentialList<E>
extends AbstractList<E>{ }
源代码图示(图下面有解释说明):
英文注释分别阐述了以下内容(一个段落用一个小标):
⑴、抽象类AbstractSequentialList提供了接口List的基本实现,它代表了顺序访问(它的底层数据结构是链表,链表只能通过前一个元素访问后一个元素),最大程度地减少了实现一个顺序访问数据存储接口(比如链表)所需的精力。对于随机访问,应优先使用抽象类AbstractList(它的底层数据结构是数组,数组能随机访问)。
⑵、抽象类AbstractSequentialList也有类似"随机访问"的方法(在某种意义上,它的随机访问实现和抽象类AbstractList是相反的),比如:
①、get(int index)
②、set(int index, E element)
③、add(int index, E element)
④、remove(int index)
这些方法不是真正意义上的随机访问,因为它们每次执行都需要从列表迭代器的开始处遍历。比如,返回索引值为6的元素,需要遍历前面5个元素,返回索引值为31的元素,需要遍历前面30个元素(数组不会这么麻烦,直接根据索引值访问元素,与前后的元素没关系)。
⑶、如果想实现一个顺序访问列表,只需要继承抽象类AbstractSequentialList,并提供方法listIterator(),size()的实现,如果想实现一个不可修改的顺序访问列表,只需要实现列表迭代器的以下方法:hasNext(),next(),hasPrevious(),previous()和index()。
⑷、如果想实现一个可变大小的顺序访问列表,只需要继承抽象类AbstractSequentialList,并提供方法set(int index, E element),remove(int index),add(int index, E element)的实现。
⑸、按照接口Collection规范中的建议,抽象类AbstractSequentialList提供了一个无参数的构造函数。
⑹、抽象类AbstractSequentialList是Java Collections Framework中的重要一员。
1、AbstractSequentialList()
//protected修饰表示仅提供给子类继承使用,不对外公开
//它是抽象类AbstractSequentialList唯一的构造方法
protected AbstractSequentialList() { }
2、get(int index)
//获取索引为index的元素
public E get(int index) {
try {
//以下代码包含了三个步骤
//①、返回从索引index开始的列表迭代器(匿名)
//...listIterator(index)
//②、获取列表迭代器游标指向的元素(匿名)
//...listIterator(index).next()
//③、返回元素
return listIterator(index).next();
//如果元素不存在,捕捉NoSuchElementException异常
} catch (NoSuchElementException exc) {
//如果索引index越界
//抛出IndexOutOfBoundsException异常
throw new IndexOutOfBoundsException("Index: "+index);
}
}
3、set(int index, E element)
//用元素element替换索引为index的元素并返回
public E set(int index, E element) {
try {
//返回从索引index开始的列表迭代器e
ListIterator<E> e = listIterator(index);
//获取列表迭代器e游标指向的元素oldVal
E oldVal = e.next();
//通过e替换元素
e.set(element);
//返回oldVal
return oldVal;
//如果元素不存在,捕捉NoSuchElementException异常
} catch (NoSuchElementException exc) {
//如果索引index越界
//抛出IndexOutOfBoundsException异常
throw new IndexOutOfBoundsException("Index: "+index);
}
}
4、add(int index, E element)
//将元素element添加到索引为index的位置
public void add(int index, E element) {
try {
//以下代码包含了两个步骤
//①、返回从索引index开始的列表迭代器(匿名)
//...listIterator(index)
//②、添加元素element
listIterator(index).add(element);
//如果元素不存在,捕捉NoSuchElementException异常
} catch (NoSuchElementException exc) {
//如果索引index越界
//抛出IndexOutOfBoundsException异常
throw new IndexOutOfBoundsException("Index: "+index);
}
}
5、remove(int index)
//删除索引为index的元素并返回
public E remove(int index) {
try {
//返回从索引index开始的列表迭代器e
ListIterator<E> e = listIterator(index);
//获取列表迭代器e游标指向的元素outCast
E outCast = e.next();
//通过e删除元素
e.remove();
//返回outCast
return outCast;
//如果元素不存在,捕捉NoSuchElementException异常
} catch (NoSuchElementException exc) {
//如果索引index越界
//抛出IndexOutOfBoundsException异常
throw new IndexOutOfBoundsException("Index: "+index);
}
}
6、addAll(int index, Collection<? extends E> c)
//从索引为index的位置开始添加集合c
public boolean addAll(int index, Collection<? extends E> c) {
try {
//变量modified,用于表示添加是否成功,初始化为false
boolean modified = false;
//返回从索引index开始的列表迭代器e1
ListIterator<E> e1 = listIterator(index);
//返回集合c的迭代器e2
Iterator<? extends E> e2 = c.iterator();
//循环遍历c,直至e2.hasNext()结果为false
//(集合c中没有后一个可供访问的元素)
while (e2.hasNext()) {
//以下代码包含了两个步骤
//①、获取迭代器e2游标指向的元素(匿名)
//通过e1添加元素(匿名)
e1.add(e2.next());
//添加成功,modified修改为true
modified = true;
}
//返回modified
return modified;
//如果元素不存在,捕捉NoSuchElementException异常
} catch (NoSuchElementException exc) {
//如果索引index越界
//抛出IndexOutOfBoundsException异常
throw new IndexOutOfBoundsException("Index: "+index);
}
}
7、iterator()
//返回迭代器
public Iterator<E> iterator() {
//返回方法listIterator()的结果
return listIterator();
}
8、listIterator(int index)
//返回列表迭代器
//抽象方法,待子类实现
public abstract ListIterator<E> listIterator(int index);
以上方法的实现都依赖列表迭代器的实现。
PS:时间有限,有关Java SE的内容会持续更新!今天就先写这么多,如果有疑问或者有兴趣,可以加QQ:2649160693,并注明优快云,我会就博文中有疑义的问题做出解答。同时希望博文中不正确的地方各位加以指正。