简体   繁体   English

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

[英]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然后合并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()))));

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然后合并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()))));

This may not be a perfect solution but you can get the desired result.这可能不是一个完美的解决方案,但您可以获得所需的结果。

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

相关问题 Java 8 Streams - 使用流将相同类型的多个对象映射到列表 - Java 8 Streams - Map Multiple Object of same type to a list using streams Java 8 Streams:根据不同的属性多次映射同一个对象 - Java 8 Streams: Map the same object multiple times based on different properties 使用Java 8流将2个不同对象的列表连接到第三个对象的列表 - Join list of 2 different object to a list of a third object using Java 8 streams 使用Java 8流API创建String映射和排序列表 - Create a map of String and sorted list using Java 8 streams API 在java 8中合并具有不同类型和长度的多个流 - Merging multiple streams in java 8 with different types and length 过滤器 Map <string,list<object> &gt; 使用 Java 流</string,list<object> - Filter Map<String,List<Object>> Using Java Streams 删除列表中的重复项<map<string, object> &gt; 在 java 中使用流</map<string,> - Remove duplicates in List<Map<String, Object>> using Streams in java Java将返回类型强制为通用类型,而不是对象类型,以使用收集器和流构造映射 - java enforcing the return type to generic types instead of Object types for constructing map using collectors and streams 使用 Java 8 Streams 从另一个创建对象列表 - Create list of object from another using Java 8 Streams Java Map 与列表值使用流列出? - Java Map with List value to list using streams?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM