首页 技术 正文
技术 2022年11月21日
0 收藏 310 点赞 2,883 浏览 2337 个字

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

部门对象:

使用java8的StreamAPI对集合计算进行代码重构

员工对象:

使用java8的StreamAPI对集合计算进行代码重构

模拟数据:

    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循环了,之后再进行操作,这样重构就完美了!!!

最后结果:

使用java8的StreamAPI对集合计算进行代码重构

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894