一、Iterable
1.定义
public interface Iterable<T>{
Iterator <T> iterator(); //抽象类中的抽象方法
default void forEach(Consumer<? super T>action){ ... }
default Spliterator<T> spliterator(){ ... } //接口中的默认方法 == 抽象类中的非抽象方法
}
提供了一个泛型接口<T>;
<T>中的T表示被迭代(遍历)的元素类型。
2.方法
public interface Iterable<T> {
Iterator<T> iterator();
}
提供了一个iterator方法,返回一个Iterable对象:用来迭代的对象(被称为迭代器)。
二、Collection
1.定义
public interface Collection<E> extends Iterable<E> { ... }
Collection是一个继承了Iterable接口的接口,故而所有Collection都具备迭代器功能。
2.方法
(1)返回容器中的元素个数
int size();
(2)判断容器是否为空
boolean isEmpty();
(3)容器中是否包含元素o
boolean contains(Object o);
(4)把元素e放入容器中
boolean add(E e);
(5)把容器中一个和o相等的元素去掉
boolean remove(Object o);
(6)把c容器中的元素都放到当前容器中
boolean addAll(Collection c);
(7)清空容器中的所有元素
void clear();
三、List
1.定义
public interface List<E> extends Collection<E> { ... }
List是一个继承了Collection接口的接口,故而所有List都具备迭代器能力,且具备装元素的能力。
线性表:逻辑结构中的线性结构,包括顺序表和链表。
2.方法
(1)尾插
boolean add(E e);
(2)将元素插入到指定位置
void add(int index, E element);
(3)删除第一个遇到的与o相等的元素
boolean remove(Object o);
(4)将index位置的元素从表中取出并返回
E remove(int index);
(5)将容器c中的所有元素按一定的顺序尾插到表中
boolean addAll(Collection c);
(6)以c作为元素的比较器,衡量元素大小原地排序
void sort(Comparator<E> c);
(7)返回index位置的元素
E get(int index);
(8)用element替换index位置的元素并返回index位置原来的元素
E set(int index,E element);
(9)从前到后,第一个与e相等的元素的位置
int indexOf(E e);
(10)从后往前,最后一个与e相等的元素的位置
int lastIndexOf(E e);
(11)将原有的List中的元素截取[fromIndex, toIndex)中的元素并返回一个新的List
List<E> subList(int fromIndex,int toIndex);