【问题标题】:Group List of Object based on few variables基于少数变量的对象组列表
【发布时间】:2021-12-22 11:38:04
【问题描述】:

我有 Employee 表,其中包含列 dept、dept_id、floor、bonus_appl、first_nm、last_nm、salary、experience。

等效的 Pojo 看起来像

class Employee{
    String department;
    String departmentId;
    int floor;
    Boolean isBonusApplicable;
    String firstName;
    String lastName;
    Long salary;
    int experience;    
}

我可以使用 JDBC 模板从数据库中获取结果集并创建员工列表。

现在,我想使用 Stream 将这个类的所有公共变量(即 departmentdepartment ID 等)组合在一起,这样我的 Pojo 就不会重复公共变量

{
    "department" : [
        {
            "department" : "HR",
            "departmentId" : "EMP_SECTION_1",
            "floor" : 2,
            "isBonusApplicable" : false,
            "employees": [
                {
                    "firstName" : "sunil",
                    "lastName"  : "kumar",
                    "salary"    : 100000,
                    "experience" : 10
                },
                {
                    "firstName" : "Anitha",
                    "lastName"  : "Shri",
                    "salary"    : 55000,
                    "experience" : 7
                }
            ]

        },
        {
            "department" : "Network",
            "departmentId" : "EMP_SECTION_7",
            "floor" : 1,
            "isBonusApplicable" : true,
            "employees": [
                {
                    "firstName" : "Harish",
                    "lastName"  : "Shah",
                    "salary"    : 12000,
                    "experience" : 1
                },
                {
                    "firstName" : "Vignesh",
                    "lastName"  : "Murthy",
                    "salary"    : 25000,
                    "experience" : 3
                }
            ]
        }    
    ]     
}

有人可以帮助我如何用一段代码将它们组合在一起。流式 API 新手

List<Employee>  employeeList = dao.getEmployeeList();
System.out.println(employeeList.size());  //returns 276 employee details spanning across 7 different departments

【问题讨论】:

    标签: java json collections java-stream pojo


    【解决方案1】:

    必须实施单独的 POJO 来存储部门和相关的员工列表。

    下面的示例使用 Lombok 注释 @Data@AllArgsConstructor 来指示实现了适当的 getter/setters/constructors 以及方法 hashCodeequals 被覆盖,这对于使用 DepartmentDto 很重要作为中间映射中的键。

    @Data
    @AllArgsConstructor
    class DepartmentDto {
        String department;
        String departmentId;
        int floor;
        Boolean isBonusApplicable; 
    
        List<EmployeeDto> employees;
    }
    
    @Data
    @AllArgsConstructor
    class EmployeeDto {
        String firstName;
        String lastName;
        Long salary;
        int experience;  
    }
    

    那么List&lt;Employee&gt;可以转换成List&lt;DepartmentDto&gt;

    List<DepartmentDto> depts = employees.stream() // Stream<Employee>
        .collect(Collectors.groupingBy(
            emp -> new DepartmentDto(
                emp.getDepartment(), emp.getDepartmentId(),
                emp.getFloor(), emp.isBonusApplicable(), null
            ), // key - DepartmentDto
            Collectors.mapping(
                emp -> new EmployeeDto(
                    emp.getFirstName(), emp.getLastName(),
                    emp.getSalary(), emp.getExperience()
                ),
                Collectors.toList() 
            ) // value -> List<EmployeeDto>
        ))  // Map<DepartmentDto, List<EmployeeDto>>
        .entrySet()
        .stream()
        .map(e -> new DepartmentDto( // copy and set employee list
                e.getKey().getDepartment(), e.getKey().getDepartmentId(),
                e.getKey().getFloor(), e.getKey().isBonusApplicable(),
                e.getValue()
        ))
        .collect(Collectors.toList());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      相关资源
      最近更新 更多