JDK 1.7源码阅读笔记(六)集合类之AbstractCollection

来源:互联网 发布:java语言程序设计教程 编辑:程序博客网 时间:2024/04/29 22:12

前言

  (1)AbstractCollection类提供 Collection 接口的骨干实现,以最大限度地减少了实现此接口所需的工作。 AbstractSet,AbstractList,AbstractSequentialList,AbstractQueue直接继承于AbstractCollection,后面还会说它的四个子类。

  (2)要实现一个不可修改的 collection,编程人员只需扩展此类,并提供 iterator 和 size 方法的实现。(iterator 方法返回的迭代器必须实现 hasNext 和 next。
  要实现可修改的 collection,编程人员必须另外重写此类的 add 方法(否则,会抛出 UnsupportedOperationException),iterator 方法返回的迭代器还必须另外实现其 remove 方法

  (4)按照 Collection 接口规范中的建议,编程人员通常应提供一个 void (无参数)和 Collection 构造方法。
  此类中每个非抽象方法的文档详细描述了其实现。如果要实现的 collection 允许更有效的实现,则可以重写这些方法中的每个方法。  

源码:

public abstract class AbstractCollection<E> implements Collection<E> {      protected AbstractCollection() {//每个实现该抽象类的,需要提供一个空的构造函数      }      public boolean add(E object) {        throw new UnsupportedOperationException();    }    public abstract Iterator<E> iterator();//返回迭代器      public abstract int size();//容量大小      public boolean isEmpty() {//是否为空         return size() == 0;      }      public boolean contains(Object o) {//判断元素o是否在集合中          Iterator<E> it = iterator();//获得当前迭代器          if (o==null) {//o为null              while (it.hasNext())//通过迭代器迭代判断是否有null的元素                  if (it.next()==null)                      return true;          } else {//元素不为null              while (it.hasNext())                  if (o.equals(it.next()))//判断内容                      return true;          }          return false;      }      public Object[] toArray() {//返回集合内元素的数组形式          int size = size(), index = 0;        Iterator<?> it = iterator();        Object[] array = new Object[size];//按size申请数组空间         while (index < size) {            array[index++] = it.next();        }        return array;    }      //返回具体的数组元素,这里的a只是一个参考值      public <T> T[] toArray(T[] contents) {          int size = size(), index = 0;//获得集合大小          //判断集合大小和contents的大小,如果contents的大小足够大,则用contents来存储元素,         //否则新生成一个数组,注意是用反射生成的,所以其元素类型能保证         if (size > contents.length) {            Class<?> ct = contents.getClass().getComponentType();            contents = (T[]) Array.newInstance(ct, size);        }        for (E entry : this) {            contents[index++] = (T) entry;        }        if (index < contents.length) {            contents[index] = null;        }        return contents;    }      //删除元素,这里是通过迭代器的删除操作来删除元素      public boolean remove(Object o) {          Iterator<E> it = iterator();//获得迭代器,在迭代过程中做操作          if (o==null) {              while (it.hasNext()) {//是否有下一个元素                  if (it.next()==null) {                      it.remove();                      return true;                  }              }          } else {              while (it.hasNext()) {                  if (o.equals(it.next())) {//比较元素内容                      it.remove();                      return true;                  }              }          }          return false;      }      //判断c中的所有元素是否在集合中      public boolean containsAll(Collection<?> c) {          for (Object e : c)//c继承了Iterable接口,可以用foreach方法              if (!contains(e))                  return false;          return true;      }      //添加所有的元素到集合中    public boolean addAll(Collection<? extends E> collection) {        boolean result = false;        Iterator<? extends E> it = collection.iterator();        while (it.hasNext()) {            if (add(it.next())) {                result = true;            }        }        return result;    }     //执行集合的交集操作      public boolean removeAll(Collection<?> c) {          boolean modified = false;          Iterator<?> it = iterator();//获得迭代器          while (it.hasNext()) {              if (c.contains(it.next())) {//包含在内部                  it.remove();                  modified = true;              }          }          return modified;      }      //求集合的异或操作      public boolean retainAll(Collection<?> c) {          boolean modified = false;          Iterator<E> it = iterator();//获得迭代器          while (it.hasNext()) {              if (!c.contains(it.next())) {//不包含在集合c中                  it.remove();                  modified = true;              }          }          return modified;      }      //执行clear操作      public void clear() {          Iterator<E> it = iterator();//获得迭代器          while (it.hasNext()) {              it.next();              it.remove();//迭代器的删除操作          }      }      //通过迭代器打印集合      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(' ');          }      }  }  
0 0