java8怎么通过Lambda处理List集合
这篇文章给大家分享的是有关java8怎么通过Lambda处理List集合的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联公司是一家集网站建设,民和企业网站建设,民和品牌网站建设,网站定制,民和网站建设报价,网络营销,网络优化,民和网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
这篇文章主要介绍了java8如何通过Lambda处理List集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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集合
当前网址:http://pwwzsj.com/article/iieidj.html