Java8怎样通过Lambda处理List集合
小编给大家分享一下Java8怎样通过Lambda处理List集合,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
成都创新互联成都网站建设按需定制开发,是成都网站推广公司,为玻璃钢雕塑提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站建设热线:13518219792
Java 8新增的Lambda表达式,我们可以用简洁高效的代码来处理List。
1、遍历
public static void main(String[] args) { ListuserList = Lists.newArrayList(); User user1 = new User(1L, "张三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); userList.add(user1); userList.add(user2); userList.add(user3); userList.stream().forEach(user ->{ System.out.println(user.getName()); }); }
运行结果:
2、list转为Map
public static void main(String[] args) { ListuserList = Lists.newArrayList();//存放user对象集合 User user1 = new User(1L, "张三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(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里面的对象元素,以某个属性来分组
public static void main(String[] args) { ListuserList = Lists.newArrayList();//存放user对象集合 User user1 = new User(1L, "张三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "张三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(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、过滤:从集合中过滤出来符合条件的元素
public static void main(String[] args) { ListuserList = Lists.newArrayList();//存放user对象集合 User user1 = new User(1L, "张三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "张三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(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、求和:将集合中的数据按照某个属性求和
public static void main(String[] args) { ListuserList = Lists.newArrayList();//存放user对象集合 User user1 = new User(1L, "张三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "张三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(6L, "王五", 28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //取出名字为张三的用户 int totalAge = userList.stream().mapToInt(User::getAge).sum(); System.out.println("和:" + totalAge); }
运行结果:
6、从List转为Map,key与value 一 一对应
public static void main(String[] args) { ListuserList = Lists.newArrayList(); User user1 = new User(1L, "张三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "张三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(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)); }
运行结果:
看完了这篇文章,相信你对“Java8怎样通过Lambda处理List集合”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网站标题:Java8怎样通过Lambda处理List集合
网站URL:http://scyanting.com/article/joohpj.html