一般与集合框架相关的四个接口:Collection, List, Set, Map:
先看看Collection的源码:
package java.util;
public interface Collection<E> extends Iterable<E> {
// Query Operations
/**
* 返回这个集合的元素数量,如果数量大于Integer.MAX_VALUE,
* 返回Integer.MAX_VALUE
*/
int size();
/**
* 如果集合元素数量为0返回true,否则返回false
*/
boolean isEmpty();
/**
*如果集合已经存在object对象,用equals方法比较对象返回true
*/
boolean contains(Object o);
/**
* 返回集合元素的迭代器
*/
Iterator<E> iterator();
/**
*返回集合元素对象数组
*/
Object[] toArray();
/**
* 返回T类型对象数组,并保存在T类型数组a中
*/
<T> T[] toArray(T[] a);
/**
* 向集合中增加对象,如果对象已经存在于集合中,返回false。
* 这里存在是指equals成立
*/
boolean add(E e);
/**
* 从集合中移动对象
*/
boolean remove(Object o);
// Bulk Operations
/**
* 如果本集合含有集合c中所有的对象,则返回true,否则返回false(可选实现)
*/
boolean containsAll(Collection<?> c);
/**
* 把集合c中所有的元素增加到本集合中(可选实现)
*/
boolean addAll(Collection<? extends E> c);
/**
* 移除在于本集合和指定集合c中的对象(可选实现)
*/
boolean removeAll(Collection<?> c);
/**
*
*/
boolean retainAll(Collection<?> c);
/**
* 清除所有的元素
*/
void clear();
boolean equals(Object o);
int hashCode();
}
Set与List继承了Collection的所有方法,不同的是Set不允许重复,而List允许重复。
Set的源码就是继承了Collection的所有方法,而List还增加了一些自己的方法,下面是List的源码:
package java.util;
public interface List<E> extends Collection<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
/**
* 移除List中第一个equals成立的的对象元素
*/
boolean remove(Object o);
boolean containsAll(Collection<?> c);
/**
* 在List的末尾增加指定集合c中的所有元素
*/
boolean addAll(Collection<? extends E> c);
/**
* 在指定位置插入指定集合c中的所有元素
*/
boolean addAll(int index, Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
// Positional Access Operations
/**
* 返回指定位置的元素
*/
E get(int index);
/**
* 使用指定的元素element代替指定位置的元素
*/
E set(int index, E element);
/**
* 在指定位置index插入指定元素element,后面元素位置往后加
*/
void add(int index, E element);
/**
* 移动指定位置index的元素
*/
E remove(int index);
// Search Operations
/**
* 返回指定位置的元素
*/
int indexOf(Object o);
/**
*返回最后一个与指定对象相等(equals成立)的元素
*/
int lastIndexOf(Object o);
ListIterator<E> listIterator();
/**
* 返回从指定下标index开始到list的末尾的ListeIterator
*/
ListIterator<E> listIterator(int index);
// View
/**
* 返回子列表,从下标fromIndex开始到toIndex
*/
List<E> subList(int fromIndex, int toIndex);
}