Iterable接口 (java.lang.Iterable) 是Java集合的顶级接口之一。Collection接口继承Iterable,所以Collection的所有子类也实现了Iterable接口。
一个实现Iterable接口的类可以使用新的for循环,下面是一个示例:
List list = new ArrayList();
for(Object o : list){
//do something o;
}
Iterable 接口只有一个方法:
public interface Iterable<T> {
public Iterator<T> iterator();
}
怎么去实现Iterable接口以便可以使用新的for循环,在我Java泛型教程的这篇文章中Implementing the Iterable Interface可以找到解释。
转载自 并发编程网 - ifeve.com