Collection接口是绝大多数集合类的父接口.
1.Collection是java.util所有集合相关类的根接口
2.Collection表示一组对象,这些对象可以看做是Collection的元素
3.一些 Collection允许有重复的元素(如List),而另一些则不允许(如Set),一些 Collection是有序的(如List),而另一些则是无序的(如Set)
4.由public interface Collection<E> extends Iterable<E>{}我们可以看到Collection接口是继承了Iterable接口的一些特性的,所以我们先来看看Iterable接口相关的源码:
java.util.Iterable源码:
public interface Iterable<T> {
//该接口包含一个泛型迭代器,用来迭代Collection的元素
Iterator<T> iterator();
}
java.util.Iterator源码:
package java.util;
public interface Iterator<E> {
//是否有下一个元素可以迭代
boolean hasNext();
//返回下一个迭代的元素
E next();
//这里可以变迭代边删除,但是在一般的for循环遍历中,不可以边遍历边删除
void remove();
}
6.下面我们来看Collection源码:
package java.util;
public interface Collection<E> extends Iterable<E> {
/**返回此 collection 中的元素数。注意:如果此 collection 包含的元素大于 Integer.MAX_VALUE,则返回 Integer.MAX_VALUE*/
int size();
/**判断collection是否有元素*/
boolean isEmpty();
/**collection是否包含某个元素.注意:当且仅当此 collection 至少包含一个满足 (o==null ? e==null : o.equals(e)) 的元素 e 时,返回 true
,可见,包含null的前提是此collection中也必须包含一个null
*/
boolean contains(Object o);
/**返回在此 collection 的元素上进行迭代的迭代器。
注意:如果collection的实例中元素是无序的,则素返回的顺序没有任何保证
*/
Iterator<E> iterator();
/**返回包含此 collection 中所有元素的数组.
注意:1.此数组是new出来的,并不维持对原有数据的引用
2.如果元素的迭代器保证了元素是有序的,则返回的数组中的对象也是有序的
*/
Object[] toArray();
/**将collection元素装入指定数组并返回
注意:1.若a能容纳所有元素,则返回包含此 collection 元素的数组
2.若a能不能容纳所有元素, 则会残生一个新数组,用来a和此collection的所有元素
*/
<T> T[] toArray(T[] a);
/**添加元素,如果此 collection 由于添加而发生更改,则返回 true。
注意:1.如果此 collection 不允许有重复元素,并且已经包含了指定的元素,则返回 false。
2.一些 collection 拒绝添加 null 元素(如),个人觉得最好所有的collectio集合拒绝添加null元素
3.如果 collection 由于某些原因(已经包含该元素的原因除外)拒绝添加特定的元素,那么它必须 抛出一个异常(而不是返回 false)
*/
boolean add(E e);
/**从此 collection 中移除指定元素,如果此 collection 包含一个或多个满足 (o==null ? e==null : o.equals(e)) 的元素 e,则移除这样的元素。
如果移除成功,或者此 collection 由于调用而发生更改,则返回 true
注意:抛出的异常.ClassCastException.NullPointerException.UnsupportedOperationException
*/
boolean remove(Object o);
/**是否包含某个集合里面的所有元素,如果此 collection 包含指定 collection 中的所有元素,则返回 true*/
boolean containsAll(Collection<?> c);
/**将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
如果在添加的过程中时同时有某个进程修改指定的 非空collection,那么这个方法不一定能确保执行成功(一般不建议这么做)
*/
boolean addAll(Collection<? extends E> c);
/**移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
此调用返回后,collection 中将不包含任何与指定 collection 相同的元素*/
boolean removeAll(Collection<?> c);
/**保留此 collection 中那些也包含在指定 collection 的元素
换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。*/
boolean retainAll(Collection<?> c);
/**移除此 collection 中的所有元素*/
void clear();
/**比较此 collection 与指定对象是否相等
jdk源注释文档中关于此方法说了许多,个人总结如下:
1.除非自己需要实现值比较,而不是引用比较,否则不建议改写此方法
3.比较时,该方法的相等必须是相等的,也就是说若q.equals(b)==true则b.equals(a)
4.自己重写一个实现该接口的集合,但不是List或者Set的派生集合时,重写此方法一定要注意第一点,同时,对于list或者Set,只能是list和
list相等,set和set相等,自己重写一个实现Collection的集合类时,如果调用常规的equals方法,将该类与任何列表或 set 进行比较时,
常规的 equals 方法必须返回 false.
5.无法写出一个同时实现正确实现 Set 和 List 接口的类
*/
boolean equals(Object o);
/**返回此 collection 的哈希码值。
注意:1.当 Collection 接口没有为 Object.hashCode 方法的常规协定添加任何约束时,为了满足 Object.hashCode 方法的常规协定,
程序员应该注意任何重写 Object.equals 方法的类必须重写 Object.hashCode 方法。
需要特别指出的是,c1.equals(c2) 暗示着 c1.hashCode()==c2.hashCode()。 */
int hashCode();
}