Most visited

Recently visited

Added in API level 1

Iterable

public interface Iterable

java.lang.Iterable<T>
Known Indirect Subclasses


实现这个接口允许一个对象成为“for-each loop”语句的目标。 For-each Loop

Summary

Public methods

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

对所有元素进行处理或操作抛出异常之前,对 Iterable每个元素执行给定操作。

abstract Iterator<T> iterator()

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

default Spliterator<T> spliterator()

创建一个 Spliterator覆盖此 Iterable描述的元素。

Public methods

forEach

Added in API level 24
void forEach (Consumer<? super T> action)

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

实现要求:
  • 默认实现的行为如下:

    for (T t : this)
             action.accept(t);
     
Parameters
action Consumer: The action to be performed for each element
Throws
NullPointerException if the specified action is null

iterator

Added in API level 1
Iterator<T> iterator ()

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

Returns
Iterator<T> an Iterator.

spliterator

Added in API level 24
Spliterator<T> spliterator ()

创建一个 Spliterator覆盖此 Iterable所描述的元素。

实现要求:
  • The default implementation creates an early-binding spliterator from the iterable's Iterator. The spliterator inherits the fail-fast properties of the iterable's iterator.
Implementation Note:
  • The default implementation should usually be overridden. The spliterator returned by the default implementation has poor splitting capabilities, is unsized, and does not report any spliterator characteristics. Implementing classes can nearly always provide a better implementation.
Returns
Spliterator<T> a Spliterator over the elements described by this Iterable.

Hooray!