java新特性之Stream,演变过程

java新特性之Stream,演变过程,第1张

遍历数组1.0

    public static void ergodicList(List list){

        for (int i = 0; i < list.size(); i++) {

            System.out.println(list.get(i));
        }


    }

看一下编译出的字节码

 

遍历List1.5

  public static void ergodicList(List list){

        for (Integer temp : list) {

            System.out.println(temp);
        }


    }

编译结果

遍历List 2.0

    public static void ergodicList(List list){

        Stream stream = list.stream();

        // 使用consumer接口实现方法
        Consumer consumer = new Consumer() {

            @Override
            public void accept(Integer temp) {
                System.out.print(temp);
            }
        };

        stream.forEach(consumer);


    }

编译结果

 

遍历List 2.5



    public static void ergodicList(List list){

        Stream stream = list.stream();

        Consumer consumer = (num) -> {
            System.out.print(num);
        };



        stream.forEach(consumer);


    }

编译结果

遍历List 3.0


    public static void ergodicList(List list){

        Stream stream = list.stream();

        Consumer consumer = num -> System.out.print(num);

        stream.forEach(consumer);


    }

编译结果和2.5一样

 

遍历List 3.5


    public static void ergodicList(List list){

        Stream stream = list.stream();

        stream.forEach(System.out::print);

    }

编译结果

 

遍历List 4.0


    public static void ergodicList(List list){

        list.stream().forEach(System.out::print);

    }

编译结果和3.5一样

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/904997.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存