JDK源码(1.7) -- java.util.AbstractList<E>

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

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

java.util.AbstractList<E>是一个抽象类,它的定义如下:

 1 public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
 2   //constructor
 3 
 4   // Modification Operations
 5 
 6   // Search Operations
 7 
 8   // Bulk Operations
 9 
10   // Iterators、subList
11 
12   // Comparison and hashing
13 
14   //inner class 'Itr'
15 
16   //inner class 'ListItr'
17 }
18 
19 class SubList<E> extends AbstractList<E> {
20 }
21 
22 class RandomAccessSubList<E> extends SubList<E> implements RandomAccess{
23 
24 }

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

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

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

java.util.List<E>接口

java.util.AbstractCollection<E>接口

下面来看看一幅图:

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

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

从下面的表格中可以看出java.util.AbstractList<E>接口中一共有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<E> iterator() 将集合中的元素以Iterator的形式返回
public ListIterator<E> listIterator() 将集合中的元素以ListIterator的形式返回
public ListIterator<E> listIterator(final int index) 将集合中的元素以ListIterator的形式返回
public List<E> subList(int fromIndex, int toIndex) 返回集合中索引在fromIndex(包括)和toIndex(不包括)之间的所有元素
比较和哈希操作 public boolean equals(Object o) 比较对象o与此集合对象是否相等
public int hashCode() 返回集合对象的hashCode

java.util.AbstractList<E>从java.util.AbstractCollection<E>继承的方法如下:

  1. public boolean addAll(Collection<? extends E> c)
  2. public boolean contains(Object o)
  3. public boolean containsAll(Collection<?> c)
  4. public boolean isEmpty()
  5. public boolean remove(Object o)
  6. public boolean removeAll(Collection<?> c)
  7. public boolean retainAll(Collection<?> c)
  8. public abstract int size()
  9. public Object[] toArray()
  10. public <T> T[] toArray(T[] a)
  11. public String toString()

java.util.AbstractList<E>从java.util.List<E>继承的方法如下:

  1. boolean addAll(int index,Collection<? extends E> c)
  2. boolean contains(Object o)
  3. boolean containsAll(Collection<?> c)
  4. boolean isEmpty()
  5. boolean remove(Object o)
  6. boolean removeAll(Collection<?> c)
  7. boolean retainAll(Collection<?> c)
  8. int size()
  9. Object[] toArray()
  10. <T> T[] toArray(T[] a)

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

下面来看看java.util.AbstractList<E>中源码部分:

一、构造函数

1     protected AbstractList() {
2     }

二、具体方法

修改操作

(1) public boolean add(E e)

源代码如下:

1     public boolean add(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(int index, E element) {
2         //直接抛出异常,需要由子类重新此方法
3         throw new UnsupportedOperationException();
4     }

(3) public void add(int index, E element)

源代码如下:

1     public void add(int index, E element) {
2         //直接抛出异常,需要由子类重写此方法
3         throw new UnsupportedOperationException();
4     }

(4) public E remove(int index)

源代码如下:

1     public E remove(int index) {
2         //直接抛出异常,需要由子类重写此方法
3         throw new UnsupportedOperationException();
4     }

(5) protected void removeRange(int fromIndex,int toIndex)

源代码如下:

1     protected void removeRange(int fromIndex, int toIndex) {
2         //返回集合对象从索引fromIndex位置开始的ListIterator对象
3         ListIterator<E> it = listIterator(fromIndex);
4         //循环上面的ListIterator对象进行删除
5         for (int i=0, n=toIndex-fromIndex; i<n; i++) {
6             it.next();
7             it.remove();
8         }
9     }

查询操作

(1) public int indexOf(Object o)

源代码如下:

 1     public int indexOf(Object o) {
 2         //返回此集合的ListIterator对象
 3         ListIterator<E> it = listIterator();
 4 
 5         if (o==null) {
 6             //如果o对象为null,则在ListIterator对象中查找
 7             while (it.hasNext())
 8                 if (it.next()==null)
 9                     return it.previousIndex();
10         } else {
11             //如果o对象不为null,则在ListIterator对象中查找
12             while (it.hasNext())
13                 if (o.equals(it.next()))
14                     return it.previousIndex();
15         }
16         return -1;
17     }

(2) public int lastIndexOf(Object o)

源代码如下:

 1     public int lastIndexOf(Object o) {
 2         //返回集合的ListIterator对象
 3         ListIterator<E> it = listIterator(size());
 4 
 5         if (o==null) {
 6             //如果o为null,则在ListIterator中往前查询
 7             while (it.hasPrevious())
 8                 if (it.previous()==null)
 9                     return it.nextIndex();
10         } else {
11             //如果o为null,则在ListIterator中往前查询
12             while (it.hasPrevious())
13                 if (o.equals(it.previous()))
14                     return it.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 void clear() {
2         //调用内部方法removeRange()来完成的
3         removeRange(0, size());
4     }

removeRange(int fromIndex,int toIndex)源代码如下:

 1     protected void removeRange(int fromIndex, int toIndex) {
 2         //返回此集合的ListIterator对象
 3         ListIterator<E> it = listIterator(fromIndex);
 4 
 5         //利用for循环删除集合中索引位置从fromIndex开始到toIndex之间的全部元素
 6         for (int i=0, n=toIndex-fromIndex; i<n; i++) {
 7             it.next();
 8             it.remove();
 9         }
10     }

(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         return modified;
11     }

Iterator和subList操作(详情看后面关于java.util.AbstractList<E>内部类的介绍)

(1) public Iterator<E> iterator()

源代码如下:

1     public Iterator<E> iterator() {
2         //每次调用iterator方法都会去new一个Itr类的实例
3         return new Itr();
4     }

(2) public ListIterator<E> listIterator()

源代码如下:

1     public ListIterator<E> listIterator() {
2         //内部是去调用listIterator(int index)方法
3         return listIterator(0);
4     }

(3) public ListIterator<E> listIterator(final int index)

源代码如下:

1  public ListIterator<E> listIterator(final int index) {
2         //检测参数index是否合法
3         rangeCheckForAdd(index);
4         //new一个ListItr类实例
5         return new ListItr(index);
6     }

(4) public List<E> subList(int fromIndex, int toIndex)

源代码如下:

1     public List<E> subList(int fromIndex, int toIndex) {
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 boolean equals(Object o) {
 2         if (o == this)
 3             return true;
 4         if (!(o instanceof List))
 5             return false;
 6 
 7         ListIterator<E> 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 int hashCode() {
2         int hashCode = 1;
3         for (E e : this)
4             hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
5         return hashCode;
6     }

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

下面来看看java.util.AbstractList<E>中的两个内部类:

内部类 : Itr

类的定义如下:

1 private class Itr implements Iterator<E> {
2    //属性
3 
4    //方法
5 }

可以知道内部类Itr实现了Iterator接口(点击查看java.util.Iterator<E>接口的相关信息)

 1     private class Itr implements Iterator<E> {
 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 boolean hasNext() {
15             return cursor != size();
16         }
17 
18         //返回迭代的下一个元素
19         public E next() {
20             //检查fast-fail机制
21             checkForComodification();
22             try {
23                 int i = cursor;
24                 //调用java.util.AbstractList<E>的get(int index)方法获取集合中index位置的元素值
25                 E next = get(i);
26                 //设置最近调用next()方法返回元素的索引值
27                 lastRet = i;
28                 cursor = i + 1;
29                 return next;
30             } catch (IndexOutOfBoundsException e) {
31                 checkForComodification();
32                 throw new NoSuchElementException();
33             }
34         }
35         
36         //从迭代器指向的collection中移除迭代器返回的最后一个元素
37         public void remove() {
38             if (lastRet < 0)
39                 throw new IllegalStateException();
40 
41             //检查fast-fail机制
42             checkForComodification();
43 
44             try {
45                 //删除元素
46                 AbstractList.this.remove(lastRet);
47                 if (lastRet < cursor)
48                     cursor--;
49                 //重置为-1
50                 lastRet = -1;
51                 expectedModCount = modCount;
52             } catch (IndexOutOfBoundsException e) {
53                 throw new ConcurrentModificationException();
54             }
55         }
56         //检查fast-fail机制,如果不满足,则抛出异常
57         final void checkForComodification() {
58             if (modCount != expectedModCount)
59                 throw new ConcurrentModificationException();
60         }
61     }

内部类 : ListItr

类的定义如下:

1 private class ListItr extends Itr implements ListIterator<E> {
2    //方法
3 }

可以知道内部类ListItr继承了Itr类并且实现了ListIterator接口(点击查看java.util.ListIterator<E>接口的相关信息)

 1     private class ListItr extends Itr implements ListIterator<E> {
 2         //带参数构造函数
 3         ListItr(int index) {
 4             cursor = index;
 5         }
 6         //如果以逆向遍历列表集合,列表迭代器有多个元素,则返回true
 7         public boolean hasPrevious() {
 8             return cursor != 0;
 9         }
10 
11         //返回列表集合中前一个元素
12         public E previous() {
13             //检查fast-fail机制
14             checkForComodification();
15             try {
16                 int i = cursor - 1;
17                 //获取前一个元素
18                 E previous = get(i);
19                 lastRet = cursor = i;
20                 return previous;
21             } catch (IndexOutOfBoundsException e) {
22                 checkForComodification();
23                 throw new NoSuchElementException();
24             }
25         }
26         //返回对next的后续调用所返回的元素的索引
27         public int nextIndex() {
28             return cursor;
29         }
30         //返回对previous的后续调用所返回元素的索引
31         public int previousIndex() {
32             return cursor-1;
33         }
34         //用指定元素替换next或者previous返回的最后一个元素
35         public void set(E e) {
36             if (lastRet < 0)
37                 throw new IllegalStateException();
38             checkForComodification();
39 
40             try {
41                 AbstractList.this.set(lastRet, e);
42                 expectedModCount = modCount;
43             } catch (IndexOutOfBoundsException ex) {
44                 throw new ConcurrentModificationException();
45             }
46         }
47         //将指定的元素插入列表
48         public void add(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 new ConcurrentModificationException();
59             }
60         }
61     }

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

 

 

posted @ 2017-01-24 15:45  火爆泡菜  阅读(1329)  评论(0编辑  收藏  举报