Java8如何通过Lambda处理List集合
这篇文章主要介绍了java8如何通过Lambda处理List集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java8新增的Lambda表达式,我们可以用简洁高效的代码来处理List。
1、遍历
publicstaticvoidmain(String[]args){ ListuserList=Lists.newArrayList(); Useruser1=newUser(1L,"张三",24); Useruser2=newUser(2L,"李四",27); Useruser3=newUser(3L,"王五",21); userList.add(user1); userList.add(user2); userList.add(user3); userList.stream().forEach(user->{ System.out.println(user.getName()); }); }
运行结果:
2、list转为Map
publicstaticvoidmain(String[]args){ ListuserList=Lists.newArrayList();//存放user对象集合 Useruser1=newUser(1L,"张三",24); Useruser2=newUser(2L,"李四",27); Useruser3=newUser(3L,"王五",21); userList.add(user1); userList.add(user2); userList.add(user3); //ID为key,转为Map Map userMap=userList.stream().collect(Collectors.toMap(User::getId,a->a,(k1,k2)->k1)); System.out.println(userMap); }
运行结果:
3、将List分组:List里面的对象元素,以某个属性来分组
publicstaticvoidmain(String[]args){ ListuserList=Lists.newArrayList();//存放user对象集合 Useruser1=newUser(1L,"张三",24); Useruser2=newUser(2L,"李四",27); Useruser3=newUser(3L,"王五",21); Useruser4=newUser(4L,"张三",22); Useruser5=newUser(5L,"李四",20); Useruser6=newUser(6L,"王五",28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //根据name来将userList分组 Map >groupBy=userList.stream().collect(Collectors.groupingBy(User::getName)); System.out.println(groupBy); }
运行结果:
4、过滤:从集合中过滤出来符合条件的元素
publicstaticvoidmain(String[]args){ ListuserList=Lists.newArrayList();//存放user对象集合 Useruser1=newUser(1L,"张三",24); Useruser2=newUser(2L,"李四",27); Useruser3=newUser(3L,"王五",21); Useruser4=newUser(4L,"张三",22); Useruser5=newUser(5L,"李四",20); Useruser6=newUser(6L,"王五",28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //取出名字为张三的用户 List filterList=userList.stream().filter(user->user.getName().equals("张三")).collect(Collectors.toList()); filterList.stream().forEach(user->{ System.out.println(user.getName()); }); }
运行结果:
5、求和:将集合中的数据按照某个属性求和
publicstaticvoidmain(String[]args){ ListuserList=Lists.newArrayList();//存放user对象集合 Useruser1=newUser(1L,"张三",24); Useruser2=newUser(2L,"李四",27); Useruser3=newUser(3L,"王五",21); Useruser4=newUser(4L,"张三",22); Useruser5=newUser(5L,"李四",20); Useruser6=newUser(6L,"王五",28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //取出名字为张三的用户 inttotalAge=userList.stream().mapToInt(User::getAge).sum(); System.out.println("和:"+totalAge); }
运行结果:
6、从List转为Map,key与value一一对应
publicstaticvoidmain(String[]args){ ListuserList=Lists.newArrayList(); Useruser1=newUser(1L,"张三",24); Useruser2=newUser(2L,"李四",27); Useruser3=newUser(3L,"王五",21); Useruser4=newUser(4L,"张三",22); Useruser5=newUser(5L,"李四",20); Useruser6=newUser(6L,"王五",28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); Map userMap=userList.stream().collect(Collectors.toMap(User::getId,user->user)); System.out.println("toMap:"+JSONArray.toJSONString(userMap)); }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。