https://blog.csdn.net/qq_43717113/article/details/105062570
forEachRemaining()是java1.8新增的Iterator接口中的默认方法
对于这个方法,官方文档是这么描述的:
Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.
简单来说,就是对集合中剩余的元素进行操作(void-T函数签名),直到元素完毕或者抛出异常。这里重要的是剩余元素,怎么理解呢。
看看源码:
/** * Performs the given action for each remaining element until all elements * have been processed or the action throws an exception. Actions are * performed in the order of iteration, if that order is specified. * Exceptions thrown by the action are relayed to the caller. * * @implSpec * <p>The default implementation behaves as if: * <pre>{@code * while (hasNext()) * action.accept(next()); * }</pre> * * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }