• forEachRemaining


    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());
        }
    }

     

    加油,愿被这世界温柔以待 ^_^
  • 相关阅读:
    2012第50周星期日
    2012第51周星期一
    2012第51周星期三
    2012第51周六
    2012第52周一
    2012第51周五冬至
    2012第51周星期二
    2012第52周二
    2012年第51周日
    2012第51周星期四
  • 原文地址:https://www.cnblogs.com/liruilong/p/14820805.html
Copyright © 2020-2023  润新知