zoukankan      html  css  js  c++  java
  • 使用java8的StreamAPI对集合计算进行代码重构

    方法: 查询出所有部门成员中年龄大于30的员工姓名

    部门对象:

    员工对象:

    模拟数据:

        private static List<Dept> list=new ArrayList<Dept>();
        private static List<Employee> listEmpl01=new ArrayList<Employee>();
        private static List<Employee> listEmpl02=new ArrayList<Employee>();
        static{//初始化数据
            Employee employee1=new Employee(1,"张三",25,1);
            Employee employee2=new Employee(1,"李四",32,1);
            Employee employee3=new Employee(1,"王五",25,1);
            Employee employee4=new Employee(1,"赵六",34,2);
            Employee employee5=new Employee(1,"周七",43,2);
            Employee employee6=new Employee(1,"小明",26,2);
            Employee employee7=new Employee(1,"大熊",22,2);
            listEmpl01.add(employee1);
            listEmpl01.add(employee2);
            listEmpl01.add(employee3);
            listEmpl02.add(employee4);
            listEmpl02.add(employee5);
            listEmpl02.add(employee6);
            listEmpl02.add(employee7);
            Dept dept01=new Dept(1,"研发部",listEmpl01);
            Dept dept02=new Dept(2,"人力资源部",listEmpl02);
        }
    

      ok了,如果不使用stream,那么就要使用双重for循环来做了:

        public static void main(String[] args) {
            List<String> result = getNameByAge(list);
            System.out.println(result);
        }
        //原始查询方法
        public static List<String> getNameByAge(List<Dept> list){
            List<String> result=new ArrayList<String>();
            for (Dept dept:list) {
                List<Employee> employees = dept.getEmployees();
                for (Employee employee:employees){
                    if(employee.getAge()>30){
                        result.add(employee.getName());
                    }
                }
            }
            return result;
        }
    

      这也是之前非常普片通用的做法,这种做法其实非常繁琐,可以先去掉for循环:

        //for循环重构
        public static List<String> getNameByAgeOne(List<Dept> list){
            List<String> result=new ArrayList<String>();
            list.forEach(dept->{
                dept.getEmployees().forEach(employee->{
                    if(employee.getAge()>30){
                        result.add(employee.getName());
                    }
                });
            });
            return result;
        }
    

      然后接着重构,上面这种明显和没重构之前区别不大:

        //stream重构
        public static List<String> getNameByAgeTwo(List<Dept> list){
            List<String> result=new ArrayList<String>();
            list.stream().forEach(dept->{
                dept.getEmployees().stream().filter(employee -> employee.getAge()>30)//使用流过滤
                        .map(employee->employee.getName()) //挑选出name
                        .forEach(name->result.add(name));
            });
            return result;
        }
    

      这里就使用了stream流,先将部门集合转换为流,再进行迭代,取出每一个员工集合,再使用流进行过滤,取name等操作,

    但是流中是可以合并流的,即我们完全没必要使用foreach中将每一个员工集合转换成流了,直接使用合并流,而且流可以转换成集合

    ,这也就意味着可以直接返回流转成的集合了:

        //stream再重构
        public static List<String> getNameByAgeFinal(List<Dept> list){
            return list.stream().flatMap(dept -> dept.getEmployees().stream())
                    .filter(employee ->employee.getAge()>30 )
                    .map(employee -> employee.getName())
                    .collect(Collectors.toList());
        }
    }
    

      直接将流collect(Collectors.toList());返回非常方便,使用flatMap将每个部门的员工集合流合并得到所有员工的流,就可以去掉for循环了,之后再进行操作,这样重构就完美了!!!

    最后结果:

    效果都是一样的,但是代码看着高大上多了!stream确实非常强大方便!!

  • 相关阅读:
    最强JAVA核心技术群
    脚本加载整个文件夹的jar到环境变量
    TTS 中文转制 google
    vue+webpack+sass
    springboot入门程序
    vue+ui
    正则表达式
    vue全家桶router、vuex、axios
    vue基础
    springboot获取自定义配置的值、获取类型安全的自定义配置的值和profile配置
  • 原文地址:https://www.cnblogs.com/houzheng/p/9790341.html
Copyright © 2011-2022 走看看