Java8 新特性之集合: forEach(Consumer

下边是JDK1.8.0_121中接口Iterable的部分源码:

    /** * Performs the given action for each element of the {@code Iterable} * until all elements have been processed or the action throws an * exception. Unless otherwise specified by the implementing class, * actions are performed in the order of iteration (if an iteration order * is specified). Exceptions thrown by the action are relayed to the * caller. * * @implSpec * <p>The default implementation behaves as if: * <pre>{@code * for (T t : this) * action.accept(t); * }</pre> * * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

下边是翻译后的源码(本人英语渣,如有错误,请指正):

    /** * 对每个{@code Iterable}元素执行特定的操作直到所有的元素被处理或者抛出一个异常。 * 除非实现类另有定义,否则操作将被按照顺序迭代器的顺序执行(如果迭代器被指定)。 * 操作抛出的异常将传递给其调用者。 * * @implSpec * <p>默认实现类似下边这样:: * <pre>{@code * for (T t : this) * action.accept(t); * }</pre> * * @param action 每个元素将要被执行的操作 * @throws NullPointerException 如果指定的操作action是空的 * @since 1.8 */
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

下边是实际练习代码

    public static void main(String[] args) {

        int i = 0;

        List<Integer> list = new ArrayList<>();
        list.add(++i);
        list.add(++i);
        list.add(++i);
        list.add(++i);
        list.forEach(cc -> System.out.println(cc));
        list.forEach(System.err :: println);
        list.add(++i); 
        Integer[] ints = new Integer[]{};
        ints = list.toArray(ints);
        System.out.println(Arrays.asList(ints));

    }

下边是以上代码的一种输出结果

1
2
3
4
1
2
[1, 2, 3, 4, 5]
3
4

下边是正式分析部分(新手总结,如有不对,请同学们指正,共同学习)

  • 新线程 —— 根据打印顺序显示,forEach 方法开启了一个新的线程
  • 线程异步安全 —— 因为交替打印,所以线程为异步;因为在集合新增了一个元素后,返回进入forEach循环继续执行打印并没有将新元素增加上,也就是说,在这个线程开启后,外界操作对其中的集合将没有任何影响,所以它是安全的。
  • Lambda表达式 —— 该forEach方法其实是一个Lambda表达式,而Lambda表达式本质上是一个匿名方法,所以这里是一个值传递,即拷贝了一份原来的值,所以外界操作不会再影响它的值,所以“线程安全”。更多关于Lambda表达式的知识,可以看看这位大神的文章,写的很不错。
  • 注意:Java8 引入了default方法修饰符,使得接口中可以有实现的方法。
    原文作者:HelloMrZheng
    原文地址: https://blog.csdn.net/hellomrzheng/article/details/70135884
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞