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>继承的方法如下:

  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>中源码部分:

一、构造函数

     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(' ');
}
}

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

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

JDK源码(1.7) -- java.util.AbstractCollection<E>的更多相关文章

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

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

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

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

  3. JDK源码学习之 java.util.concurrent.automic包

    一.概述 Java从JDK1.5开始提供了java.util.concurrent.atomic包,方便程序员在多线程环境下无锁的进行原子操作.原子变量的底层使用了处理器提供的原子指令,但是不同的CP ...

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

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

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

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

  6. JDK源码(1.7) -- java.util.Arrays

    java.util.Arrays 源码分析 ------------------------------------------------------------------------------ ...

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

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

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

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

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

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

随机推荐

  1. AngularJS 指令绑定 & 简介

    指令中独立scope 的 & 官方说明: 1. 绑定表达式 2. 经常用来绑定回调函数 诡异的地方在于,这个 & 某次听人说在子组件中是不能传值给callback的,好奇查了一下官方文 ...

  2. jsoup抓取网页报错UnsupportedMimeTypeException

    今天在用Jsoup爬虫的时候两次遇到下面错误 Exception in thread "main" org.jsoup.UnsupportedMimeTypeException: ...

  3. Linux kernel学习-内存管理

    转自:https://zohead.com/archives/linux-kernel-learning-memory-management/ 本文同步自(如浏览不正常请点击跳转):https://z ...

  4. Android :ExpandableListActivity

    http://developer.android.com/reference/android/app/ExpandableListActivity.html# public class Expanda ...

  5. Deep Learning基础--随时间反向传播 (BackPropagation Through Time,BPTT)推导

    1. 随时间反向传播BPTT(BackPropagation Through Time, BPTT) RNN(循环神经网络)是一种具有长时记忆能力的神经网络模型,被广泛用于序列标注问题.一个典型的RN ...

  6. centos7安装Python3的过程中会和Python2.7版本冲突导致yum版本比对应,致使yum不能使用的问题。

    centos7安装Python3的过程中会和Python2.7版本冲突导致yum版本比对应,致使yum不能使用的问题. 原因:yum调用Python,启动程/usr/bin/yum就是一个python ...

  7. SwitchSharp代理插件的安装和使用

    参考链接: http://bbs.feng.com/read-htm-tid-8227283.html 安装参考链接: http://jingyan.baidu.com/article/380abd0 ...

  8. 创建一个简单的Maven工程

    Maven的工程结构如下图所示: 大致来看,Maven的工程结构如下: 在创建maven工程时,可以通过骨架创建,也可以不通过骨架创建. 我们先用idea通过骨架创建一个Maven工程. 配置pom. ...

  9. Hadoop(一):概述

    一.Hadoop是什么? Hadoop是一个由Apache基金会所开发的分布式系统基础架构.Hadoop框架最核心的设计包含两个方面,一是分布式文件系统(Hadoop Distributed File ...

  10. 转- 阿里云、Amazon、Google云数据库方案架构与技术分析

    「一切都会运行在云端」. 云时代早已来临,本文着眼于顶级云服务商云服务商的云数据库方案背后的架构,以及笔者最近观察到的一些对于云数据库有意义的工业界的相关技术的进展,希望读者能有所收获. 现在越来越多 ...