Collection接口:
1 public interface Collection<T> extends Iterable<T>
2 {
3 int size();
4 boolean isEmpty();
5 void clear();
6 boolean contains();
7 boolean add(T x);
8 boolean remove(T x);
9 java.util.Iterator<T> iterator();
10 }
Iterable接口:
1 public interface Iterable<T>
2 {
3 Iterator<T> iterator();
4 }
Iterator接口:
1 public interface Iterator<E>
2 {
3 boolean hasNext();
4 E next();
5 void remove();
6 }
由他们的定义可看出:
这三者的关系是:Collection接口扩展了Iterable接口,Iterable接口定义中只包含了一个返回类型为Iterator的方法iterator。
另外需要说一下:Java的集合类主要有两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口。这里简单说一下Collection接口。
Collection接口派生出了Set(无序集合),Queue(队列),List(有序集合)。
如果访问List集合中的元素,可以根据索引来访问,访问Set集合中的元素,只能通过访问本身。