繁体   English   中英

Java 流:使用不同 object 类型的多个列表创建 Map

[英]Java Streams: Create Map Using Multiple List of different object types

我想使用不同 object 类型的多个列表创建对象的 Map。 我想举个例子,这样会更清楚,更容易讨论。

class Employee {
Long id;
String name;
// getters, setters, and toString
}

class Department {
Long id;
String name;
// getters, setters, and toString
} 

class EmployeeDepartment {
Long id;
Long employeeId;
Long deptId;
// getters, setters, and toString
}

class Subject {
Long id;
String name;
// getters, setters, and toString
}

class EmployeeSubject {
Long id;
Long employeeId;
Long subjectId;
// getters, setters, and toString
}

我现在有

List<Employee> emplList;
List<EmployeeDepartment> emplDeptList;
List<EmployeeSubject> emplSubList;

我想获得一个 Map ,我可以使用它来搜索员工:deptId 和 subjectId

就像是

Map<List<Long, Long>, List<Employee>> deptSubToEmpMap;
// representing Map<List<deptId, subId>, List<empl>>

// Or
Map<Long, Map<Long, List<Employee>> deptToSubToEmpMap;
// representing Map<deptId, Map<subId, List<empl>>

或任何其他方便的形式,如果它比上述表示更好。

注意:我是 Java Streams 的新手

您需要创建EmployeeSubjectDepartment来保存员工、部门和主题之间的关系,如下所示,

@Getter@Setter
class EmployeeSubjectDepartment{
    Long empId;
    Long deptId;
    Long subId;

    public EmployeeSubjectDepartment(Long empId, Long deptId, Long subId) {
        this.empId = empId;
        this.deptId = deptId;
        this.subId = subId;
    }
}

然后合并emplDeptListemplSubList得到emplyees

List<EmployeeSubjectDepartment> employeeSubjectDepartments = emplDeptList.stream()
                .flatMap(dept -> emplSubList.stream()
                        .filter(sub -> dept.employeeId.equals(sub.employeeId))
                        .map(sub -> new EmployeeSubjectDepartment(dept.employeeId, dept.deptId, sub.subjectId)))
                .collect(toList());

Map<Long, Map<Long, List<Long>>> result = employeeSubjectDepartments.stream()
        .collect(groupingBy(EmployeeSubjectDepartment::getDeptId,
                groupingBy(EmployeeSubjectDepartment::getSubId, mapping(e -> e.empId, toList()))));

这将导致Map带有员工 ID List

如果您想要员工List ,请创建EmployeeSubjectDepartment ,如下所示,

 class EmployeeSubjectDepartment{
        Employee emp;
        Long deptId;
        Long subId;
    } 

然后合并emplDeptListemplSubListemplList获取和 map 到emplyees

List<EmployeeSubjectDepartment> employeeSubjectDepartments = emplDeptList.stream()
                .flatMap(dept -> emplSubList.stream()
                        .filter(sub -> dept.employeeId.equals(sub.employeeId))
                        .map(sub -> new EmployeeSubjectDepartment(emplList.stream().filter(e -> e.id == sub.employeeId).findAny().orElse(null), dept.deptId, sub.subjectId)))
                .collect(toList());

Map<Long, Map<Long, List<Employee>>> result = employeeSubjectDepartments.stream()
        .collect(groupingBy(EmployeeSubjectDepartment::getDeptId,
                groupingBy(EmployeeSubjectDepartment::getSubId, mapping(e -> e.emp, toList()))));

这可能不是一个完美的解决方案,但您可以获得所需的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM