java.util.AbstractList 源码分析(JDK1.7)

---------------------------------------------------------------------------------

java.util.AbstractList是一个抽象类,它的定义如下:
1 public abstract class AbstractList extends AbstractCollection implements List{2 //constructor3
4 //Modification Operations5
6 //Search Operations7
8 //Bulk Operations9
10 //Iterators、subList11
12 //Comparison and hashing13
14 //inner class 'Itr'15
16 //inner class 'ListItr'
17 }18
19 class SubList extends AbstractList{20 }21
22 class RandomAccessSubList extends SubList implementsRandomAccess{23
24 }

(1)从上面可以看出java.util.AbstractList好复杂哟,它不光有那么多的方法,而且还有2个内部类,这都还不算,它的类文件中居然还有两个其它的类(~_~),是不是要醉了,这都要怪它爹java.util.List提供的25个没有实现的方法(@_@)

(2)好了,不废话了,不抱怨了,继续挽起袖子往下干

继续往下面看前,可以先去了解下:

下面来看看一幅图:

java list相减 steam java list<e>_java

---------------------------------------------------------------------------------

下面来看看java.util.AbstractList中具体有哪些方法:

从下面的表格中可以看出java.util.AbstractList接口中一共有16个方法:其中查询操作3个;修改操作5个;批量操作2个;Iterator和subList操作4个;比较和哈希操作2个;

修改操作
public boolean add(E e)
将指定的元素e添加到集合尾部
public E set(int index, E element)
将集合中index位置的元素替换为element元素
public void add(int index, E element)
将指定的元素e添加到集合的index位置
public E remove(int index)
删除集合中index位置上的元素
protected void removeRange(int fromIndex,int toIndex)
删除集合中索引在fromIndex(包括)和toIndex(不包括)之间的所有元素
查询操作
public int indexOf(Object o)
返回集合中元素o第一次出现的位置index
public int lastIndexOf(Object o)
返回集合中元素o最后一次出现的位置idnex
abstract public E get(int index)
返回集合index位置上的元素
批量操作
public void clear()
清空集合中的元素
public boolean addAll(int index, Collection extends E> c)
将子集合c添加到集合尾部
Iterator和subList操作
public Iterator iterator()
将集合中的元素以Iterator的形式返回
public ListIterator listIterator()
将集合中的元素以ListIterator的形式返回
public ListIterator listIterator(final int index)
将集合中的元素以ListIterator的形式返回
public List subList(int fromIndex, int toIndex)
返回集合中索引在fromIndex(包括)和toIndex(不包括)之间的所有元素
比较和哈希操作
public boolean equals(Object o)
比较对象o与此集合对象是否相等
public int hashCode()
返回集合对象的hashCode
java.util.AbstractList从java.util.AbstractCollection继承的方法如下:
public boolean addAll(Collection extends E> c)
public boolean contains(Object o)
public boolean containsAll(Collection> c)
public boolean isEmpty()
public boolean remove(Object o)
public boolean removeAll(Collection> c)
public boolean retainAll(Collection> c)
public abstract int size()
public Object[] toArray()
public T[] toArray(T[] a)
public String toString()
java.util.AbstractList从java.util.List继承的方法如下:
boolean addAll(int index,Collection extends E> c)
boolean contains(Object o)
boolean containsAll(Collection> c)
boolean isEmpty()
boolean remove(Object o)
boolean removeAll(Collection> c)
boolean retainAll(Collection> c)
int size()
Object[] toArray()
 T[] toArray(T[] a)
---------------------------------------------------------------------------------
下面来看看java.util.AbstractList中源码部分:
一、构造函数
1 protectedAbstractList() {2 }
二、具体方法
修改操作
(1) public boolean add(E e)
源代码如下:
1 public booleanadd(E e) {2 //内部调用了add(int index,E e)方法
3 add(size(), e);4 return true;5 }
(2) public E set(int index, E element)
源代码如下:
1 public E set(intindex, E element) {2 //直接抛出异常,需要由子类重新此方法
3 throw newUnsupportedOperationException();4 }
(3) public void add(int index, E element)
源代码如下:
1 public void add(intindex, E element) {2 //直接抛出异常,需要由子类重写此方法
3 throw newUnsupportedOperationException();4 }
(4) public E remove(int index)
源代码如下:
1 public E remove(intindex) {2 //直接抛出异常,需要由子类重写此方法
3 throw newUnsupportedOperationException();4 }
(5) protected void removeRange(int fromIndex,int toIndex)
源代码如下:
1 protected void removeRange(int fromIndex, inttoIndex) {2 //返回集合对象从索引fromIndex位置开始的ListIterator对象
3 ListIterator it =listIterator(fromIndex);4 //循环上面的ListIterator对象进行删除
5 for (int i=0, n=toIndex-fromIndex; i
查询操作
(1) public int indexOf(Object o)
源代码如下:
1 public intindexOf(Object o) {2 //返回此集合的ListIterator对象
3 ListIterator it =listIterator();4
5 if (o==null) {6 //如果o对象为null,则在ListIterator对象中查找
7 while(it.hasNext())8 if (it.next()==null)9 returnit.previousIndex();10 } else{11 //如果o对象不为null,则在ListIterator对象中查找
12 while(it.hasNext())13 if(o.equals(it.next()))14 returnit.previousIndex();15 }16 return -1;17 }
(2) public int lastIndexOf(Object o)
源代码如下:
1 public intlastIndexOf(Object o) {2 //返回集合的ListIterator对象
3 ListIterator it =listIterator(size());4
5 if (o==null) {6 //如果o为null,则在ListIterator中往前查询
7 while(it.hasPrevious())8 if (it.previous()==null)9 returnit.nextIndex();10 } else{11 //如果o为null,则在ListIterator中往前查询
12 while(it.hasPrevious())13 if(o.equals(it.previous()))14 returnit.nextIndex();15 }16 return -1;17 }
(3) abstract public E get(int index)
源代码如下:
1 abstract public E get(int index);
批量操作
(1) public void clear()
源代码如下:
1 public voidclear() {2 //调用内部方法removeRange()来完成的
3 removeRange(0, size());4 }
removeRange(int fromIndex,int toIndex)源代码如下:
1 protected void removeRange(int fromIndex, inttoIndex) {2 //返回此集合的ListIterator对象
3 ListIterator it =listIterator(fromIndex);4
5 //利用for循环删除集合中索引位置从fromIndex开始到toIndex之间的全部元素
6 for (int i=0, n=toIndex-fromIndex; i
(2) public boolean addAll(int index, Collection extends E> c)
源代码如下:
1 public boolean addAll(int index, Collection extends E>c) {2 //检查参数index是否合法
3 rangeCheckForAdd(index);4 boolean modified = false;5 //利用for循环依次取出子集合c中的元素,然后添加调用add(int index,E e)方法将元素添加到集合中
6 for(E e : c) {7 add(index++, e);8 modified = true;9 }10 returnmodified;11 }
Iterator和subList操作(详情看后面关于java.util.AbstractList内部类的介绍)
(1) public Iterator iterator()
源代码如下:
1 public Iteratoriterator() {2 //每次调用iterator方法都会去new一个Itr类的实例
3 return newItr();4 }
(2) public ListIterator listIterator()
源代码如下:
1 public ListIteratorlistIterator() {2 //内部是去调用listIterator(int index)方法
3 return listIterator(0);4 }
(3) public ListIterator listIterator(final int index)
源代码如下:
1 public ListIterator listIterator(final intindex) {2 //检测参数index是否合法
3 rangeCheckForAdd(index);4 //new一个ListItr类实例
5 return newListItr(index);6 }
(4) public List subList(int fromIndex, int toIndex)
源代码如下:
1 public List subList(int fromIndex, inttoIndex) {2 //如果此集合有RandomAccess接口标记,则new一个 RandomAccessSubList类实例,否则就new一个SubList类实例
3 return (this instanceof RandomAccess ?
4 new RandomAccessSubList<>(this, fromIndex, toIndex) :5 new SubList<>(this, fromIndex, toIndex));6 }
比较和哈希操作
(1) public boolean equals(Object o)
源代码如下:
1 public booleanequals(Object o) {2 if (o == this)3 return true;4 if (!(o instanceofList))5 return false;6
7 ListIterator e1 =listIterator();8 ListIterator e2 =((List) o).listIterator();9 while (e1.hasNext() &&e2.hasNext()) {10 E o1 =e1.next();11 Object o2 =e2.next();12 if (!(o1==null ? o2==null: o1.equals(o2)))13 return false;14 }15 return !(e1.hasNext() ||e2.hasNext());16 }
(2) public int hashCode()
源代码如下:
1 public inthashCode() {2 int hashCode = 1;3 for (E e : this)4 hashCode = 31*hashCode + (e==null ? 0: e.hashCode());5 returnhashCode;6 }
----------------------------------------------------------------------------------------
下面来看看java.util.AbstractList中的两个内部类:
内部类 : Itr
类的定义如下:
1 private class Itr implements Iterator{2 //属性3
4 //方法
5 }
可以知道内部类Itr实现了Iterator接口(点击查看java.util.Iterator接口的相关信息)
1 private class Itr implements Iterator{2
3 //记录索引位置(这个索引位置就是下次调用next()方法时返回元素的位置)
4 int cursor = 0;5
6 //记录最近调用next()或者previous()方法时返回元素的索引7 //如果调用了remove()方法则将它的值重置为-1
8 int lastRet = -1;9
10 //fast-fail机制标记
11 int expectedModCount =modCount;12
13 //如果仍有元素可以迭代,则返回true
14 public booleanhasNext() {15 return cursor !=size();16 }17
18 //返回迭代的下一个元素
19 publicE next() {20 //检查fast-fail机制
21 checkForComodification();22 try{23 int i =cursor;24 //调用java.util.AbstractList的get(int index)方法获取集合中index位置的元素值
25 E next =get(i);26 //设置最近调用next()方法返回元素的索引值
27 lastRet =i;28 cursor = i + 1;29 returnnext;30 } catch(IndexOutOfBoundsException e) {31 checkForComodification();32 throw newNoSuchElementException();33 }34 }35
36 //从迭代器指向的collection中移除迭代器返回的最后一个元素
37 public voidremove() {38 if (lastRet < 0)39 throw newIllegalStateException();40
41 //检查fast-fail机制
42 checkForComodification();43
44 try{45 //删除元素
46 AbstractList.this.remove(lastRet);47 if (lastRet 
50 lastRet = -1;51 expectedModCount =modCount;52 } catch(IndexOutOfBoundsException e) {53 throw newConcurrentModificationException();54 }55 }56 //检查fast-fail机制,如果不满足,则抛出异常
57 final voidcheckForComodification() {58 if (modCount !=expectedModCount)59 throw newConcurrentModificationException();60 }61 }
内部类 : ListItr
类的定义如下:
1 private class ListItr extends Itr implements ListIterator{2 //方法
3 }
可以知道内部类ListItr继承了Itr类并且实现了ListIterator接口(点击查看java.util.ListIterator接口的相关信息)
1 private class ListItr extends Itr implements ListIterator{2 //带参数构造函数
3 ListItr(intindex) {4 cursor =index;5 }6 //如果以逆向遍历列表集合,列表迭代器有多个元素,则返回true
7 public booleanhasPrevious() {8 return cursor != 0;9 }10
11 //返回列表集合中前一个元素
12 publicE previous() {13 //检查fast-fail机制
14 checkForComodification();15 try{16 int i = cursor - 1;17 //获取前一个元素
18 E previous =get(i);19 lastRet = cursor =i;20 returnprevious;21 } catch(IndexOutOfBoundsException e) {22 checkForComodification();23 throw newNoSuchElementException();24 }25 }26 //返回对next的后续调用所返回的元素的索引
27 public intnextIndex() {28 returncursor;29 }30 //返回对previous的后续调用所返回元素的索引
31 public intpreviousIndex() {32 return cursor-1;33 }34 //用指定元素替换next或者previous返回的最后一个元素
35 public voidset(E e) {36 if (lastRet < 0)37 throw newIllegalStateException();38 checkForComodification();39
40 try{41 AbstractList.this.set(lastRet, e);42 expectedModCount =modCount;43 } catch(IndexOutOfBoundsException ex) {44 throw newConcurrentModificationException();45 }46 }47 //将指定的元素插入列表
48 public voidadd(E e) {49 checkForComodification();50
51 try{52 int i =cursor;53 AbstractList.this.add(i, e);54 lastRet = -1;55 cursor = i + 1;56 expectedModCount =modCount;57 } catch(IndexOutOfBoundsException ex) {58 throw newConcurrentModificationException();59 }60 }61 }
----------------------------------------------------------------------------------------