[英]Java Streams: Create Map Using Multiple List of different object types
I want to create a Map of objects using multiple lists of different object types.我想使用不同 object 类型的多个列表创建对象的 Map。 I want to take an example so that it'd be more clear and easy to discuss.我想举个例子,这样会更清楚,更容易讨论。
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
}
Now, I have我现在有
List<Employee> emplList;
List<EmployeeDepartment> emplDeptList;
List<EmployeeSubject> emplSubList;
I want to get a Map by which I can make a search of employee using: deptId, and subjectId我想获得一个 Map ,我可以使用它来搜索员工:deptId 和 subjectId
Something like就像是
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>>
or any other convenient form if it's better than above representation.或任何其他方便的形式,如果它比上述表示更好。
Note: I'm a newbie to Java Streams注意:我是 Java Streams 的新手
You need to create EmployeeSubjectDepartment
that will hold relation between employee, department and subject, as below,您需要创建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;
}
}
And then merge emplDeptList
and emplSubList
to get emplyees
然后合并emplDeptList
和emplSubList
得到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()))));
This will result in Map
with List
of employeeIds.这将导致Map
带有员工 ID List
。
If you want the List
of employees, create EmployeeSubjectDepartment
as below,如果您想要员工List
,请创建EmployeeSubjectDepartment
,如下所示,
class EmployeeSubjectDepartment{
Employee emp;
Long deptId;
Long subId;
}
And then merge emplDeptList
and emplSubList
to get and map to emplyees
from emplList
然后合并emplDeptList
和emplSubList
从emplList
获取和 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()))));
This may not be a perfect solution but you can get the desired result.这可能不是一个完美的解决方案,但您可以获得所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.