zoukankan      html  css  js  c++  java
  • 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());
        }
    }

     

    加油,愿被这世界温柔以待 ^_^
  • 相关阅读:
    Java中的异常处理
    Java源码阅读Vector
    Java源码中遇到的一些问题(更新中)
    Java迭代器原理
    有趣的位运算-与或非
    有趣的位运算-移位运算
    为何要使用原码, 反码和补码?
    有趣的位运算-异或
    为什么实现Serializbale接口就能够进行序列化?
    死锁,活锁,饥饿
  • 原文地址:https://www.cnblogs.com/liruilong/p/14820805.html
Copyright © 2011-2022 走看看