英文:
Group By List of object on multiple fields Java 8
问题
我将拥有一个 Employee 列表,其中将包含 3 个属性 - departmentId、employeeId、employeeGroup。
我想在部门 ID 和员工 ID 上执行分组操作。想法是发送员工所属部门的组名。以下是可能的 3 种情况:
**情况#1:同一部门,不同员工,不同组。**
["Dept001", 100, "Admin"]
["Dept001", 101, "Contrator"]
**情况#2:同一部门,相同员工,不同组**
["Dept001", 100, "Admin"]
["Dept001", 100, "Contrator"]
**情况#3:不同部门,不同员工,相同组。**
["Dept001", 100, "Admin"]
["Dept002", 101, "Admin"]
我尝试了以下内容:
Map<String, Set<Employee>> map = new TreeMap<>();
map = myList.stream().collect(Collectors.groupingBy(Employee::getDepartmentId,
Collectors.toSet()));
英文:
I will have a list of Employee, which will hold 3 attributes - departmentId, employeeId, employeeGroup.
I want to perform group by on Department Id and employee id. The idea is to send the group name of employee with belonging department. Below can be the 3 possible cases:
Case#1: Same department, different employee, different group.
["Dept001", 100, "Admin"]
["Dept001", 101, "Contrator"]
Case#2: Same department, same employee, different group
["Dept001", 100, "Admin"]
["Dept001", 100, "Contrator"]
Case#3: Diff department, diff employee, same group.
["Dept001", 100, "Admin"]
["Dept002", 101, "Admin"]
I have tried below:
Map<String, Set<Employee>> map = new TreeMap<>();
map = myList.stream().collect(Collectors.groupingBy(Employee::getDepartmentId,
Collectors.toSet()));
答案1
得分: 2
按多个字段分组将需要多次调用Collectors#groupingBy
。幸运的是,重载的Collectors#groupingBy
方法的第二个参数是另一个Collector
,因此可以简单地调用两次。
Map<String, Map<Integer, Set<Employee>>> map = myList.stream()
.collect(Collectors.groupingBy(Employee::getDepartmentId,
Collectors.groupingBy(Employee::getEmployeeId, Collectors.toSet())));
请注意Map
的值是另一个Map
;第一个Map
的键是员工的部门ID,第二个Map
的键是员工的员工ID。
英文:
Grouping by multiple fields is going to require multiple calls to Collectors#groupingBy
. Luckily, the second parameter of the overloaded Collectors#groupingBy
method is another Collector
, so it can simply be invoked twice.
Map<String, Map<Integer, Set<Employee>>> map = myList.stream()
.collect(Collectors.groupingBy(Employee::getDepartmentId,
Collectors.groupingBy(Employee::getEmployeeId, Collectors.toSet())));
Note that the value of the Map
is another Map
; the first of which is keyed with the employees' department ID, and the second is keyed with the employees' employee ID.
答案2
得分: 0
根据我的理解,您只需使用EmployeeId
和DepartmentId
进行groupBy
,以便随后可以使用这个组合属性唯一地标识一个员工。
您可以使用stream
与Tuple
结合,代码如下所示:
Map<Tuple, List<Employee>> map = employees.stream()
.collect(groupingBy(employee -> new Tuple(employee.getEmployeeId(), employee.getDepartmentId())));
英文:
As I understand, you just want to do groupBy
with EmployeeId
and DepartmentId
so that afterward you can uniquely identify an employee with this combined property.
You can use stream
combined with Tuple
, code will look like this:
Map<Tuple, List<Emplyee>> map = employees.stream()
.collect(groupingBy(employee-> new Tuple(employee.getEmployeeId(), employee.getDepartmentId())));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论