Collection
Collection是List和Set集合的根接口,该接口规定了一些操作集合的基本方法,List和Set接口都继承了Collection中的所有方法,但List接口还另外规定了一些方法,Set接口基本没怎么变化。
//Collection接口部分源码
public interface Collection<E> extends Iterable<E>{
int size(); //返回元素的个数
boolean isEmpty(); //判断集合是否为空
---------------------------------------------------------------------------------
boolean add(E e); //添加元素
boolean addAll(Collection<? extends E> c);
---------------------------------------------------------------------------------
boolean contains(Object o); //判断集合是否包含指定的元素
boolean containsAll(Collection<?> c);
---------------------------------------------------------------------------------
boolean remove(Object o); //删除元素
boolean removeAll(Collection<?> c); //类似集合的差运算
default boolean removeIf(Predicate<? super E> filter);
---------------------------------------------------------------------------------
boolean retainAll(Collection<?> c); //类似集合的交运算
---------------------------------------------------------------------------------
void clear();
---------------------------------------------------------------------------------
Iterator<E> iterator(); //获得集合的迭代器对象
---------------------------------------------------------------------------------
Object[] toArray(); //集合转数组
//将集合转成数组,其元素存储在指定的数组中,若数组大小过小,则会重新分配一个大小合适的数组,若数组过大,多余的元素会被填充为 null。
<T> T[] toArray(T[] a);
---------------------------------------------------------------------------------
boolean equals(Object o);
int hashCode();
}
List
该集合元素有序,可重复,有下标。
//List接口部分源码
public interface List<E> extends Collection<E> {
//该接口继承了所有Collection接口的方法,这里不再赘述,只列出了部分新增的方法
E get(int index); //根据索引获取元素
E set(int index, E element); //根据索引设置元素
----------------------------------------------------------------
int indexOf(Object o); //获取指定元素第一次出现的索引
int lastIndexOf(Object o); //获取指定元素最后一次出现的索引
----------------------------------------------------------------
ListIterator<E> listIterator(int index); //获取专门操作List集合的迭代器对象
----------------------------------------------------------------
void add(int index, E element); //在指定索引处添加元素
boolean addAll(int index, Collection<? extends E> c);
----------------------------------------------------------------
E remove(int index); //删除指定索引处的元素
----------------------------------------------------------------
List<E> subList(int fromIndex, int toIndex); //获取指定区间的子列表,左闭右开
----------------------------------------------------------------
default void sort(Comparator<? super E> c) { //根据指定的排序器排序
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
}
Set
该集合元素无序,不重复,无下标。
public interface Set<E> extends Collection<E> {
//该接口继承了所有Collection接口的方法,并未扩展自己的新方法,所以详细方法请参考Collection。
}
相关的接口
Iterator
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
ListIterator
public interface ListIterator<E> extends Iterator<E> {
boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove();
void set(E e);
void add(E e);
}
Map
该集合存储键值对。键值对无序,无下标,键唯一不可重复,值可重复。
//Map接口部分源码
public interface Map<K,V> {
int size();
boolean isEmpty();
-------------------------------------------------------
void clear();
-------------------------------------------------------
boolean containsKey(Object key);
boolean containsValue(Object value);
-------------------------------------------------------
V get(Object key);
V put(K key, V value);
void putAll(Map<? extends K, ? extends V> m);
-------------------------------------------------------
V remove(Object key); //删除键是key的键值对
-------------------------------------------------------
default boolean replace(K key, V oldValue, V newValue); //将键是key,值是oldValue的键值对的值替换为newValue
-------------------------------------------------------
Set<K> keySet(); //获取全部键
Collection<V> values(); //获取全部值
Set<Map.Entry<K, V>> entrySet(); //获取键值对,键值对封装在entry中
-------------------------------------------------------
boolean equals(Object o);
int hashCode();
}
相关的接口
Entry
interface Entry<K,V> {
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
}
Collections工具类
Collection是一个工具类,提供了一些常用的操作集合的方法
//方法实在是太多,这里是列举几个最为常用的方法
public static void reverse(List<?> list); //反转列表中的元素
public static void shuffle(List<?> list); //打乱元素的顺序
public static void sort(List<T> list); //排序,列表的元素必须实现Comprable接口