java.util.AbstractCollection<E> 源码分析(JDK1.7)
---------------------------------------------------------------------------------
java.util.AbstractCollection<E>是一个抽象类,它的定义如下:
public abstract class AbstractCollection<E> implements Collection<E> {
//construct //Query Operations // Modification Operations // Bulk Operations // String conversion
}
(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>继承的方法如下:
- boolean equals(Object o);
- 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>中源码部分:
一、构造函数
protected AbstractCollection() {
}
二、具体方法
查询操作
(1) public abstract Iterator<E> iterator()
源代码如下:(抽象方法,由具体的子类提供实现)
public abstract Iterator<E> iterator();
(2) public abstract int size()
源代码如下:(抽象方法,由具体的子类提供实现)
public abstract int size();
(3) public boolean isEmpty()
源代码如下:
public boolean isEmpty() {
return size() == 0;
}
(4) public boolean contains(Object o)
源代码如下:
public boolean contains(Object o) {
//返回此集合的Iterator对象
Iterator<E> it = iterator(); if (o==null) {
//比较对象o为null,则循环Iterator查找是否有对象为null
while (it.hasNext())
if (it.next()==null)
return true;
} else {
//比较对象o不为null,则循环Iterator查找是否有对象与o相等
while (it.hasNext())
if (o.equals(it.next()))
return true;
}
return false;
}
(5) public Object[] toArray()
源代码如下:
public Object[] toArray() {
//创建一个Object类型的数组,数组大小为Collection中元素的个数
Object[] r = new Object[size()];
//返回此collection的Iterator对象
Iterator<E> it = iterator();
//利用循环将Iterator中的对象赋值给Object数组
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) // fewer elements than expected
return Arrays.copyOf(r, i);
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
(6) public <T> T[] toArray(T[] a)
源代码如下:
public <T> T[] toArray(T[] a) {
// Estimate size of array; be prepared to see more or fewer elements
int size = size();
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator<E> it = iterator(); for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) { // fewer elements than expected
if (a != r)
return Arrays.copyOf(r, i);
r[i] = null; // null-terminate
return r;
}
r[i] = (T)it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
修改操作
(1) public boolean add(E e)
源代码如下:(没有提供具体的实现,调用此方法会抛出异常)
public boolean add(E e) {
throw new UnsupportedOperationException();
}
(2) public boolean remove(Object o)
源代码如下:
public boolean remove(Object o) {
//返回Collection的Iterator对象
Iterator<E> it = iterator(); if (o==null) {
//要删除的对象为null,则循环Iterator查找对象为null,并且删除掉
while (it.hasNext()) {
if (it.next()==null) {
it.remove();
return true;
}
}
} else {
//要删除的对象不为null,则循环Iterator查找对象,并且删除掉
while (it.hasNext()) {
if (o.equals(it.next())) {
it.remove();
return true;
}
}
}
return false;
}
批量操作
(1) public boolean containsAll(Collection<?> c)
源代码如下:
public boolean containsAll(Collection<?> c) {
//循环取出collection中的每个对象,然后去调用contains()方法
for (Object e : c)
if (!contains(e))
return false;
return true;
}
(2) public boolean addAll(Collection<? extends E> c)
源代码如下:
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
//循环取出要添加的子collection中的元素,然后去调用add()方法
for (E e : c)
if (add(e))
modified = true;
return modified;
}
(3) public boolean removeAll(Collection<?> c)
源代码如下:
public boolean removeAll(Collection<?> c) {
boolean modified = false;
//返回collection的Iterator对象
Iterator<?> it = iterator();
//依次循环取出Iterator中的对象,然后调用contains()方法查看该对象是否在collectoin中,如果存在的话,则调用remove()方法删除掉
while (it.hasNext()) {
if (c.contains(it.next())) {
it.remove();
modified = true;
}
}
return modified;
}
(4) public boolean retainAll(Collection<?> c)
源代码如下:
public boolean retainAll(Collection<?> c) {
boolean modified = false;
//返回Collection的Iterator对象
Iterator<E> it = iterator();
//依次循环取出Iterator中的对象,然后调用cotains()方法查看该对象是否在collection中,如果不存在的话则调用,则调用remove()方法删除掉
while (it.hasNext()) {
if (!c.contains(it.next())) {
it.remove();
modified = true;
}
}
return modified;
}
(5) public void clear()
源代码如下:
public void clear() {
//返回collection集合的Iterator对象
Iterator<E> it = iterator();
//依次循环Iterator取出每一个对象,然后调用remove()方法删除掉
while (it.hasNext()) {
it.next();
it.remove();
}
}
字符串描述
(1) public String toString()
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]"; StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------