源码分析一(Iterator、Collection以及List接口)

1:Iterable接口,实现这个接口的类对象可以进行迭代

 1 package java.lang;
 2 import java.util.Iterator;
 3 /**
 4  * 实现这个接口的类所创建的对象可以进行迭代
 5  */
 6 public interface Iterable<T> {
 7 
 8     /**
 9      * 调用这个方法会返回迭代器,迭代器可以理解为操纵杆,可以操作某个对象
10      */
11     Iterator<T> iterator();
12 }

2:Collection类,它是List以及Set的父类

  1 package java.util;
  2 
  3 /**
  4  * 这个接口继承了Iterable接口,那么它的子孙类实例化时就可以迭代输出
  5  */
  6 
  7 public interface Collection<E> extends Iterable<E> {
  8     // Query Operations
  9 
 10     /**
 11      * 获取集合长度
 12      * 返回这个集合中元素的个数,如果集合中包含元素数量超过整数所能表示的最大值,那么返回这个最大值,
 13      * 这种超过最大值的情况基本上不存在,如果存在,内存应该早就溢出了。
 14      */
 15     int size();
 16 
 17     /**
 18      * 判断集合是否为空
 19      * 如果集合中没有任何元素,返回true
 20      */
 21     boolean isEmpty();
 22 
 23     /**
 24      * 判断集合中是否包含某个元素
 25      * 如果集合中包含指定的元素,返回true,集合中至少包含一个元素,可以为null或者其他元素
 26      * 比较方式:o==null?e==null:o.equals(e)
 27      */
 28     boolean contains(Object o);
 29 
 30     /**
 31      * 这个方法返回一个迭代器,可以对集合中元素进行遍历
 32      */
 33     Iterator<E> iterator();
 34 
 35     /**
 36      * 将集合转化为数组
 37      * 返回一个数组,如果集合中元素是有序的,那么返回的数组也是相同的顺序
 38      * 这个方法为集合和数组之间搭建了互相转换的桥梁
 39      */
 40     Object[] toArray();
 41 
 42     /**
 43      * 此方法返回一个指定类型的数组,如果指定的数组的容量大于集合的容量,那么直接将集合中的
 44      * 元素放到数组中返回,否则重新创建一个新数组
 45      */
 46     <T> T[] toArray(T[] a);
 47 
 48     // Modification Operations
 49 
 50     /**
 51      * 添加元素到集合中,添加成功返回true,否则返回false
 52      */
 53     boolean add(E e);
 54 
 55     /**
 56      * 从集合中移除某个对象,移除成功返回true,否则返回false
 57      */
 58     boolean remove(Object o);
 59 
 60 
 61     // Bulk Operations
 62 
 63     /**
 64      * 判断集合中是否包含指定集合中所有元素,是返回true,否则返回false
 65      */
 66     boolean containsAll(Collection<?> c);
 67 
 68     /**
 69      * 添加指定集合的所有元素到集合中,成功返回true,否则返回false
 70      */
 71     boolean addAll(Collection<? extends E> c);
 72 
 73     /**
 74      * 在当前集合中删除指定集合中包含的元素,成功返回true,否则返回false
 75      */
 76     boolean removeAll(Collection<?> c);
 77 
 78     /**
 79      * 保留指定集合中包含的元素,换句话说就是删除指定集合中没有的元素
 80      */
 81     boolean retainAll(Collection<?> c);
 82 
 83     /**
 84      * 清空集合中所有的元素
 85      */
 86     void clear();
 87 
 88 
 89     // Comparison and hashing
 90 
 91     /**
 92      * 比较两个对象是否相等
 93      */
 94     boolean equals(Object o);
 95 
 96     /**
 97      * 获取hashcode的值
 98      */
 99     int hashCode();
100 }

3:List集合继承了Collection集合

  1 package java.util;
  2 
  3 public interface List<E> extends Collection<E> {
  4 
  5      
  6     int size();
  7     
  8     boolean isEmpty();
  9 
 10     boolean contains(Object o);
 11 
 12     Iterator<E> iterator();
 13 
 14     Object[] toArray();
 15 
 16     <T> T[] toArray(T[] a);
 17 
 18     boolean add(E e);
 19 
 20     boolean remove(Object o);
 21 
 22     boolean containsAll(Collection<?> c);
 23     
 24     boolean addAll(Collection<? extends E> c);
 25     /**
 26      * 在指定位置index插入指定集合的元素
 27      */
 28     boolean addAll(int index, Collection<? extends E> c);
 29 
 30     boolean removeAll(Collection<?> c);
 31 
 32     boolean retainAll(Collection<?> c);
 33 
 34     void clear();
 35 
 36 
 37     boolean equals(Object o);
 38 
 39     int hashCode();
 40 
 41     /**
 42      * 根据索引index获取指定的元素
 43      * @param index
 44      * @return
 45      */
 46     E get(int index);
 47 
 48     /**
 49      * 替换指定位置的元素,返回这个位置的元素
 50      * @param index
 51      * @param element
 52      * @return
 53      */
 54     E set(int index, E element);
 55 
 56     /**
 57      * 在指定位置插入元素,索引处元素依次向右移动
 58      * @param index
 59      * @param element
 60      */
 61     void add(int index, E element);
 62 
 63     /**
 64      * 移除指定位置的元素
 65      * @param index
 66      * @return
 67      */
 68     E remove(int index);
 69 
 70 
 71     // Search Operations
 72 
 73     /**
 74      * 返回集合中第一次出现指定元素的索引,如果没有返回-1
 75      */
 76     int indexOf(Object o);
 77 
 78     /**
 79      * 返回指定元素的索引,从最后一个元素开始查找
 80      * @param o
 81      * @return
 82      */
 83     int lastIndexOf(Object o);
 84 
 85 
 86     // List Iterators
 87 
 88     /**
 89      * 返回一个list的迭代器
 90      */
 91     ListIterator<E> listIterator();
 92 
 93     /**
 94      * 返回一个list的迭代器,从指定位置开始
 95      */
 96     ListIterator<E> listIterator(int index);
 97 
 98     // View
 99 
100     /**
101      * 返回一个子集,从fromIndex开始到toIndex结束
102      */
103     List<E> subList(int fromIndex, int toIndex);
104 }
原文地址:https://www.cnblogs.com/warrior4236/p/6550731.html