zoukankan      html  css  js  c++  java
  • Java xxList.stream().collect(Collectors.groupingBy(User::getCategory)); List对象集合按对象属性分组、分组汇总、过滤等操作示例

    userList.stream().collect(Collectors.groupingBy(User::getCategory));使用详解及扩展。直接上代码
    (底部附带异常)
    @Test
        void contextLoads1() {
            User user0 = new User();
            user0.setId(0);
            user0.setNickname("name0");
            user0.setUsername("username0");
            user0.setCategory("学生");
    
            User user1 = new User();
            user1.setId(1);
            user1.setNickname("name1");
            user1.setUsername("username1");
            user1.setCategory("老师");
    
            User user2 = new User();
            user2.setId(2);
            user2.setNickname("name2");
            user2.setUsername("username2");
            user2.setCategory("教学主任");
    
            List<User> userList = new ArrayList<>();
            userList.add(user0);
            userList.add(user1);
            userList.add(user2);
    
        //跟据某个属性分组
            Map<String, List<User>> userMap = userList.stream().collect(Collectors.groupingBy(User::getCategory));
            System.out.println(userMap);
         // 查询结果 
    // {学生=[User{id=0, nickname=name0, birthday=null, username=username0, password=null, status=null, createTime=null, updateTime=null, remarks=null}], // 老师=[User{id=1, nickname=name1, birthday=null, username=username1, password=null, status=null, createTime=null, updateTime=null, remarks=null}], // 教学主任=[User{id=2, nickname=name2, birthday=null, username=username2, password=null, status=null, createTime=null, updateTime=null, remarks=null}]}
        //根据某个属性分组,汇总某个属性
        Map<String, Integer> collect2 = userList.stream().collect(Collectors.groupingBy(User::getCategory,Collectors.summingInt(User::getId)));
        System.out.println(collect2);
        // 查询结果
        // {学生=0, 老师=1, 教学主任=2}


        //根据某个属性添加条件过滤数据
        userList = userList.stream().filter(u -> !u.getNickname().equals("name0")).collect(Collectors.toList());
        System.out.println(userList);
        // 查询结果
        // [User{id=1, nickname=name1, birthday=null, username=username1, password=null, status=null, createTime=null, updateTime=null, remarks=null},
        // User{id=2, nickname=name2, birthday=null, username=username2, password=null, status=null, createTime=null, updateTime=null, remarks=null}]
        //判断一组对象里面有没有这个属性值
        boolean add = userList.stream().anyMatch(m -> "name2".equals(m.getNickname()));
        System.out.println(add);

        //取出一组对象的某个属性组成一个新集合
        List<String> Username=userList.stream().map(User::getUsername).collect(Collectors.toList());
        System.out.println(Username);
        // 查询结果  
        // [username0, username1, username2]
    
    

    User实体类

    public class User implements Serializable {
    
        private static final long serialVersionUID=1L;
    
        /**
         * 主键
         */
        private Integer id;
    
        /**
         * 昵称
         */
        private String nickname;
        /**
         * 类别
         */
        private String category;
    
        /**
         * 生日
         */
        private LocalDate birthday;
    
        /**
         * 用户名
         */
        private String username;
    
        /**
         * 密码
         */
        private String password;
    
        /**
         * 状态 
         */
        private String status;
    
        /**
         * 创建时间
         */
        private LocalDateTime createTime;
    
        /**
         * 更新时间
         */
        private LocalDateTime updateTime;
    
        /**
         * 备注
         */
        private String remarks;
      
      // 省略set/get      
    }

     element cannot be mapped to a null key 

     / / 按照 User 分组,此时 getCategory 有空值,就会报错。

     // 改为,增加非空过滤。

  • 相关阅读:
    pat 甲级 1065. A+B and C (64bit) (20)
    pat 甲级 1064. Complete Binary Search Tree (30)
    pat 甲级 1010. Radix (25)
    pat 甲级 1009. Product of Polynomials (25)
    pat 甲级 1056. Mice and Rice (25)
    pat 甲级 1078. Hashing (25)
    pat 甲级 1080. Graduate Admission (30)
    pat 甲级 团体天梯 L3-004. 肿瘤诊断
    pat 甲级 1099. Build A Binary Search Tree (30)
    Codeforce 672B. Different is Good
  • 原文地址:https://www.cnblogs.com/liyh321/p/14343518.html
Copyright © 2011-2022 走看看