如何使用Java流在groupby之后应用排序和限制 [英] How to apply sorting and limiting after groupby using Java streams

查看:81
本文介绍了如何使用Java流在groupby之后应用排序和限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下需要根据员工部门进行分组的员工数据列表,然后我想找到每个部门中收入最高的2位员工.

I have the following list of Employee data which I need to group based on the employee department and then I want to find the 2 highest-paid employees in each department.

public class Employee {

    private int id;
    private String name;
    private String dept;
    private int salary;

    //setters and getters
}

List<Employee> listOfEmp = new ArrayList<>();

listOfEmp.add(new Employee (1, "A", "IT",100));
listOfEmp.add(new Employee (2, "B", "IT",200));
listOfEmp.add(new Employee (3, "C", "SUPPORT",100));
listOfEmp.add(new Employee (4, "D", "SUPPORT",200));
listOfEmp.add(new Employee (5, "E", "SUPPORT",300));
listOfEmp.add(new Employee (6, "F", "SUPPORT",400));
listOfEmp.add(new Employee (7, "G", "IT",500));
listOfEmp.add(new Employee (8, "H", "IT",600));
listOfEmp.add(new Employee (9, "I", "IT",700));

以下是我按部门写给小组员工的查询

following is the query I have written to group employees by department

Map<String, List<String>> departmentWiseEmployees  = listOfEmp.stream().
        collect(Collectors.groupingBy(Employee::getDept, Collectors.mapping(Employee::getName, toList())));

我正在尝试使用以下方法使用以下方法查找每个部门中薪水最高的2名员工.但这是

I am trying to use the following method to find the highest 2 paid employees in each department using the following method. but it is

int limit = 2;
    Map<String, List<String>> groupByTeachers =
            listOfEmp.stream()
                    .collect(
                            Collectors.groupingBy(
                                    Employee::getDept,
                                    Collectors.collectingAndThen(
                                            Collectors.toList(),
                                            e -> e.stream().sorted().limit(limit).collect(toList() ) ) ) );

此处编译器抱怨为collectingAndThen提供的lambda参数出现以下错误,它说e为List<Employee>类型,但必须为List<Object>

here compiler complains about following error on the lambda parameter provided for collectingAndThen, it says e is of type List<Employee> but it has to be List<Object>

有人可以帮助我了解这里出了什么问题吗?

Could somebody help me to understand what is going wrong here?

推荐答案

这部分代码:

e -> e.stream().sorted().limit(limit).collect(Collectors.toList())

返回List<Employee>而不是List<String>的列表,因此您可以将结果的类型更改为:

return List of List<Employee> and not List<String>, so either you change the type of the result to:

Map<String, List<Employee>> groupByTeachers = 
                // ...The rest of your code

或者如果您期望使用Map<String, List<String>>,请更改collectingAndThen以获取期望的字段,例如getNamegetDep:

Or if you expect Map<String, List<String>>, change the collectingAndThen to get the expected field, for example getName or getDep:

e -> e.stream().sorted().limit(limit)
        .map(Employee::getDep) // for example getDep
        .collect(Collectors.toList())

这篇关于如何使用Java流在groupby之后应用排序和限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆