普通for循环、增强for循环、普通foreach、stream foreach的区别

复制代码
总结(>代表好于):
1.性能:一万左右数据 普通for = parallelstream foreach > 增强版for > 普通foreach > stream foreach
       十万左右及以上数据 普通for = 增强版for < 普通foreach < stream foreach < parallelstream foreach
2.continue break 在普通for环境和增强for循环中可以使用,在两种foreach中不能使用
3.return 在普通for环境和增强for循环中是指定方法返回值,而在两种foreach中是起到continue类似的作用
4.入参都可以是空集合,都不能是null
java 复制代码
package com.xin.demo.streamdemo;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class StreamForeachTest {
    public static void main(String[] args) {
        List<String> nameList = new ArrayList<>();
        nameList.add("xiaohua1");
        nameList.add("xiaohua2");
        nameList.add("xiaohua3");
        nameList.add("xiaohua4");
        nameList.add("xiaohua5");

        // 1.普通for循环
        // 可以接受空集合,但是不能是null,否则nameList.size()会报空指针异常
        // 普通for循环里可以有continue,break,return是用来指定方法返回值的
        getCommonList(nameList);

        // 2.增强版for循环
        // 可以接受空集合,但是不能是null,否则会报空指针异常
        // java5引入,增强版for循环里可以有continue,break,return是用来指定方法返回值的
        getEnhanceList(nameList);

        // 3.普通forEach
        // 可以接受空集合,但是不能是null,否则会报空指针异常
        // 不可以使用continue和break,return不是用来指定方法返回值的,
        // 而是结束本次循环开始下次循环,相当于continue
        getCommonForEach(nameList);

        // 4.stream forEach
        // 可以接受空集合,但是不能是null,否则会报空指针异常
        // 不可以使用continue和break,return不是用来指定方法返回值的,
        // 而是结束本次循环开始下次循环,相当于continue
        getStreamForEach(nameList);

        // 总结(>代表好于):
        // 1.性能:一万左右数据 普通for = parallelstream foreach > 增强版for > 普通foreach > stream foreach
        //       十万左右及以上数据 普通for = 增强版for < 普通foreach < stream foreach < parallelstream foreach
        // 2.continue break 在普通for环境和增强for循环中可以使用,在两种foreach中不能使用
        // 3.return 在普通for环境和增强for循环中是指定方法返回值,而在两种foreach中是起到continue类似的作用
        // 4.入参都可以是空集合,都不能是null

    }

    public static List<String> getCommonList(List<String> nameList) {
        List<String> commonNameList = new ArrayList<>();
        for (int i = 0; i < nameList.size(); i++) {
            if (StringUtils.equals(nameList.get(i), "xiaohua2")) {
                commonNameList.add(nameList.get(i));
                continue;
            }

            if (StringUtils.equals(nameList.get(i), "xiaohua3")) {
                commonNameList.add(nameList.get(i));

                return commonNameList;
            }
        }

        System.out.println(commonNameList);
        return commonNameList;
    }

    public static List<String> getEnhanceList(List<String> nameList) {
        List<String> commonNameList = new ArrayList<>();
        for (String name : nameList) {
            if (StringUtils.equals(name, "xiaohua2")) {
                commonNameList.add(name);
                continue;
            }

            if (StringUtils.equals(name, "xiaohua3")) {
                commonNameList.add(name);

                return commonNameList;
            }
        }

        System.out.println(commonNameList);
        return commonNameList;
    }

    public static List<String> getCommonForEach(List<String> nameList) {
        List<String> commonNameList = new ArrayList<>();
        nameList.forEach(name -> {
            if (StringUtils.equals(name, "xiaohua2")) {
                commonNameList.add(name);
                return;
            }

            if (StringUtils.equals(name, "xiaohua3")) {
                commonNameList.add(name);

                // 这里会报错
//                return commonNameList;

            }
        });

        System.out.println(commonNameList);
        return commonNameList;
    }

    public static List<String> getStreamForEach(List<String> nameList) {
        List<String> commonNameList = new ArrayList<>();
        nameList.stream().forEach(name -> {
            if (StringUtils.equals(name, "xiaohua2")) {
                commonNameList.add(name);
                return;
            }

            if (StringUtils.equals(name, "xiaohua3")) {
                commonNameList.add(name);

                // 这里会报错
//                return commonNameList;

            }
        });

        System.out.println(commonNameList);
        return commonNameList;
    }
}
相关推荐
胚芽鞘6811 分钟前
查询依赖冲突工具maven Helper
java·数据库·maven
Charlie__ZS7 分钟前
若依框架去掉Redis
java·redis·mybatis
咖啡啡不加糖40 分钟前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
玩代码1 小时前
Java线程池原理概述
java·开发语言·线程池
NE_STOP1 小时前
SpringBoot--如何给项目添加配置属性及读取属性
java
水果里面有苹果1 小时前
20-C#构造函数--虚方法
java·前端·c#
%d%d21 小时前
python 在运行时没有加载修改后的版本
java·服务器·python
金銀銅鐵1 小时前
[Kotlin] 单例对象是如何实现的?
java·kotlin
泰勒疯狂展开1 小时前
Java研学-MongoDB(三)
java·开发语言·mongodb
zzywxc7871 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt