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

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

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

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

 1 public abstract class AbstractCollection<E> implements Collection<E> {
 2    //construct 
 3    
 4    //Query Operations
 5  
 6    // Modification Operations
 7 
 8    // Bulk Operations
 9 
10    //  String conversion
11 }

(1)java.util.AbstractCollection<E>提供了对java.util.Collection<E>接口的骨干实现,以最大限度地减少了实现Collection接口所需要的工作。

(2)按照Collection接口规范中的建议,通常应提供一个void(无参数)和Collection构造方法。

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

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

从下面的表格中可以看出java.util.AbstractCollection<E>接口中一共有14个方法,其中查询操作6个;修改操作2个;批量操作5个;字符串描述1个

查询操作 public abstract Iterator<E> iterator() 返回在此collection中的元素上进行迭代的迭代器
public abstract int size() 返回此collection中的元素数,如果此collection包含的元素大于Integer.MAX_VALUE,则返回Integer.MAX_VALUE
public boolean isEmpty() 如果此collection不包含元素,则返回true
public boolean contains(Object o) 如果此collection包含指定的元素,则返回true
public Object[] toArray() 返回包含此collection中所有元素的数组
public <T> T[] toArray(T[] a) 返回包含此collection中所有元素的数组
修改操作 public boolean add(E e) 确保此collection包含指定的元素
public boolean remove(Object o) 从此collection中移除指定元素的单个实例
批量操作 public boolean containsAll(Collection<?> c) 如果此collection包含指定collection中的所有元素,则返回true
public boolean addAll(Collection<? extends E> c) 将指定collection中的所有元素都添加到此collection中
public boolean removeAll(Collection<?> c) 移除此collection中那些也包含在指定collection中的所有元素
public boolean retainAll(Collection<?> c) 仅保留此collection中那些也包含在指定collection的元素
public void clear() 移除此collection中的所有元素
字符串描述 public String toString() 返回此collection的字符串表示形式

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

  1. boolean equals(Object o);
  2. int hashCode();

再看下面的图示:

java.util.Collection<E>接口中一共定义了15个方法,java.util.AbstractCollection<E>对其中的11个方法提供了实现,其中iterator()、size()、equals()、hashCode()4个方法没有提供实现,需要由java.util.AbstractCollection<E>的扩展类来提供具体的实现。

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

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

一、构造函数

1     protected AbstractCollection() {
2     }

二、具体方法

查询操作

(1) public abstract Iterator<E> iterator()

源代码如下:(抽象方法,由具体的子类提供实现)

1 public abstract Iterator<E> iterator();

(2) public abstract int size()

源代码如下:(抽象方法,由具体的子类提供实现)

1 public abstract int size();

(3) public boolean isEmpty()

源代码如下:

1     public boolean isEmpty() {
2         return size() == 0;
3     }

(4) public boolean contains(Object o)

源代码如下:

 1     public boolean contains(Object o) {
 2         //返回此集合的Iterator对象
 3         Iterator<E> it = iterator();
 4 
 5         if (o==null) {
 6             //比较对象o为null,则循环Iterator查找是否有对象为null
 7             while (it.hasNext())
 8                 if (it.next()==null)
 9                     return true;
10         } else {
11             //比较对象o不为null,则循环Iterator查找是否有对象与o相等
12             while (it.hasNext())
13                 if (o.equals(it.next()))
14                     return true;
15         }
16         return false;
17     }

(5) public Object[] toArray()

源代码如下:

 1     public Object[] toArray() {
 2         //创建一个Object类型的数组,数组大小为Collection中元素的个数
 3         Object[] r = new Object[size()];
 4         //返回此collection的Iterator对象
 5         Iterator<E> it = iterator();
 6         //利用循环将Iterator中的对象赋值给Object数组
 7         for (int i = 0; i < r.length; i++) {
 8             if (! it.hasNext()) // fewer elements than expected
 9                 return Arrays.copyOf(r, i);
10             r[i] = it.next();
11         }
12         return it.hasNext() ? finishToArray(r, it) : r;
13     }

(6) public <T> T[] toArray(T[] a)

源代码如下:

 1     public <T> T[] toArray(T[] a) {
 2         // Estimate size of array; be prepared to see more or fewer elements
 3         int size = size();
 4         T[] r = a.length >= size ? a :
 5                   (T[])java.lang.reflect.Array
 6                   .newInstance(a.getClass().getComponentType(), size);
 7         Iterator<E> it = iterator();
 8 
 9         for (int i = 0; i < r.length; i++) {
10             if (! it.hasNext()) { // fewer elements than expected
11                 if (a != r)
12                     return Arrays.copyOf(r, i);
13                 r[i] = null; // null-terminate
14                 return r;
15             }
16             r[i] = (T)it.next();
17         }
18         return it.hasNext() ? finishToArray(r, it) : r;
19     }

修改操作

(1) public boolean add(E e)

源代码如下:(没有提供具体的实现,调用此方法会抛出异常)

1     public boolean add(E e) {
2         throw new UnsupportedOperationException();
3     }

(2) public boolean remove(Object o)

源代码如下:

 1     public boolean remove(Object o) {
 2         //返回Collection的Iterator对象
 3         Iterator<E> it = iterator();
 4 
 5         if (o==null) {
 6             //要删除的对象为null,则循环Iterator查找对象为null,并且删除掉
 7             while (it.hasNext()) {
 8                 if (it.next()==null) {
 9                     it.remove();
10                     return true;
11                 }
12             }
13         } else {
14            //要删除的对象不为null,则循环Iterator查找对象,并且删除掉
15             while (it.hasNext()) {
16                 if (o.equals(it.next())) {
17                     it.remove();
18                     return true;
19                 }
20             }
21         }
22         return false;
23     }        

批量操作

(1) public boolean containsAll(Collection<?> c)

源代码如下:

1     public boolean containsAll(Collection<?> c) {
2         //循环取出collection中的每个对象,然后去调用contains()方法
3         for (Object e : c)
4             if (!contains(e))
5                 return false;
6         return true;
7     }

(2) public boolean addAll(Collection<? extends E> c)

源代码如下:

1     public boolean addAll(Collection<? extends E> c) {
2         boolean modified = false;
3         //循环取出要添加的子collection中的元素,然后去调用add()方法
4         for (E e : c)
5             if (add(e))
6                 modified = true;
7         return modified;
8     }

(3) public boolean removeAll(Collection<?> c)

源代码如下:

 1     public boolean removeAll(Collection<?> c) {
 2         boolean modified = false;
 3         //返回collection的Iterator对象
 4         Iterator<?> it = iterator();
 5         //依次循环取出Iterator中的对象,然后调用contains()方法查看该对象是否在collectoin中,如果存在的话,则调用remove()方法删除掉
 6         while (it.hasNext()) {
 7             if (c.contains(it.next())) {
 8                 it.remove();
 9                 modified = true;
10             }
11         }
12         return modified;
13     }

(4) public boolean retainAll(Collection<?> c)

源代码如下:

 1     public boolean retainAll(Collection<?> c) {
 2         boolean modified = false;
 3         //返回Collection的Iterator对象
 4         Iterator<E> it = iterator();
 5        //依次循环取出Iterator中的对象,然后调用cotains()方法查看该对象是否在collection中,如果不存在的话则调用,则调用remove()方法删除掉
 6         while (it.hasNext()) {
 7             if (!c.contains(it.next())) {
 8                 it.remove();
 9                 modified = true;
10             }
11         }
12         return modified;
13     }

(5) public void clear()

源代码如下:

1     public void clear() {
2         //返回collection集合的Iterator对象
3         Iterator<E> it = iterator();
4         //依次循环Iterator取出每一个对象,然后调用remove()方法删除掉
5         while (it.hasNext()) {
6             it.next();
7             it.remove();
8         }
9     }

字符串描述

(1) public String toString()

 1     public String toString() {
 2         Iterator<E> it = iterator();
 3         if (! it.hasNext())
 4             return "[]";
 5 
 6         StringBuilder sb = new StringBuilder();
 7         sb.append('[');
 8         for (;;) {
 9             E e = it.next();
10             sb.append(e == this ? "(this Collection)" : e);
11             if (! it.hasNext())
12                 return sb.append(']').toString();
13             sb.append(',').append(' ');
14         }
15     }

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

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

原文地址:https://www.cnblogs.com/xinhuaxuan/p/6343547.html