Iterable<T>接口

  https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html

  public interface Iterable<T>

  一、简介

  T,表示迭代器返回的元素的类型。

  一个object实现了这个接口,就能使用“for-each loop”语句。

  二、方法

  1、Iterator<T> iterator()

  返回一个T类型元素的迭代器。

  2、default void forEach(Consumer<? super T> action)

  action,代表要对每个元素执行的操作。

  对Iterable的每个元素执行给定的操作,直到处理完所有的元素或者操作抛出异常。除非实现类另有规定,否则按迭代顺序执行操作(如果指定了迭代顺序)。

  该操作引发的异常会中继给调用者。

  异常抛出,如果指定的action为空,抛出"NullPointerException"异常。

  一般的实现为:

for (T t : this)
    action.accept(t);

  

  3、default Spliterator<T> spliterator()

  根据此Iterable接口描述的元素创建Spliterator(这是一个用于遍历和分割数组、集合中元素的接口)

原文地址:https://www.cnblogs.com/bigbigbigo/p/8552961.html