码迷,mamicode.com
首页 > 编程语言 > 详细

java8新特性:利用Lambda处理List集合

时间:2020-01-21 16:25:19      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:map   sum   new   lambda   bar   结果   ima   lists   ati   

Java 8新增的Lambda表达式,我们可以用简洁高效的代码来处理List。

1、遍历

技术图片
 public static void main(String[] args) {
        List<User> userList = 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) {
        List<User> userList = 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<Long,User> 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) {
        List<User> userList = 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<String, List<User>> groupBy = userList.stream().collect(Collectors.groupingBy(User::getName));
        System.out.println(groupBy);

    }
技术图片

运行结果:

技术图片

4、过滤:从集合中过滤出来符合条件的元素

技术图片
 public static void main(String[] args) {
        List<User> userList = 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<User> 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) {
        List<User> userList = 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) {
        List<User> userList = 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<Long/*Id*/,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
        System.out.println("toMap:" + JSONArray.toJSONString(userMap));

    }
技术图片

运行结果:

技术图片

java8新特性:利用Lambda处理List集合

标签:map   sum   new   lambda   bar   结果   ima   lists   ati   

原文地址:https://www.cnblogs.com/weizhxa/p/12222000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!