我正在尝试找到每个部门最新雇员的薪水。在Java 8中,可以在一步中完成。

huangapple go评论78阅读模式
英文:

I am trying to find the Salary of the Newest Employee for every Department. Using java 8 in a single step

问题

public static void main(String[] args) {
    List<Employee> employees = new ArrayList<>();
    Employee e1 = new Employee(1, 1000, "a1", "A");
    Employee e2 = new Employee(2, 1010, "b1", "A");
    Employee e3 = new Employee(3, 1000, "a1", "B");
    Employee e4 = new Employee(4, 500, "b2", "B");
    Employee e5 = new Employee(5, 2000, "a1", "C");
    Employee e6 = new Employee(6, 5000, "b2", "C");
    employees.add(e1);
    employees.add(e2);
    employees.add(e3);
    employees.add(e4);
    employees.add(e5);
    employees.add(e6);
    Map<String, Integer> map = employees.stream()
            .collect(groupingBy(
                    Employee::getDepartment,
                    collectingAndThen(maxBy(comparingInt(Employee::getEmployeeId)), empOpt -> empOpt.map(Employee::getSalary).orElse(0))
            ));
}

Output:

{
  "a1" : 2000,
  "b1" : 1010,
  "b2" : 5000
}
英文:

Aim is to find the Salary of the newest Employee in the Department considering employee ID will increase.
I have written a 2 step code and I am trying to find a solution that can do this in a single step using java8.

My Process is:

  1. get a Map of "Department" : "Newest Employee" based on Largest EmployeeId in that Department
  2. Use the Previous Map to create "Department" : "Salary of the Newest Employee" (This is the output)

Will it be possible to do it in a single step?

    public static void main(String[] args) {
    List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();
    Employee e1 = new Employee(1, 1000, &quot;a1&quot;, &quot;A&quot;);
    Employee e2 = new Employee(2, 1010, &quot;b1&quot;, &quot;A&quot;);
    Employee e3 = new Employee(3, 1000, &quot;a1&quot;, &quot;B&quot;);
    Employee e4 = new Employee(4, 500, &quot;b2&quot;, &quot;B&quot;);
    Employee e5 = new Employee(5, 2000, &quot;a1&quot;, &quot;C&quot;);
    Employee e6 = new Employee(6, 5000, &quot;b2&quot;, &quot;C&quot;);
    employees.add(e1);
    employees.add(e2);
    employees.add(e3);
    employees.add(e4);
    employees.add(e5);
    employees.add(e6);
    //This Map will contain Department and The newest Employee in that Department.
    Map&lt;String, Employee&gt; retVal = employees.stream()
            .collect(groupingBy(
                    e -&gt; e.getDepartment(),
                    collectingAndThen(maxBy(comparingInt(e -&gt; e.getEmployeeId())), Optional::get)
            ));
    //This Map will use the previous map to construct a combination of &quot;Department&quot; : &quot;Salary of the newest Member&quot;
    Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
    retVal.entrySet().forEach(stringEmployeeEntry -&gt; {
        map.put(stringEmployeeEntry.getKey(), stringEmployeeEntry.getValue().getSalary());
    });
}

Output

{
  &quot;a1&quot; : 2000,
  &quot;b1&quot; : 1010,
  &quot;b2&quot; : 5000, 
}

答案1

得分: 2

你可以直接将collectingAndThen中的工资从Employee对象中进行映射:

Map<String, Integer> retVal = employees.stream()
        .collect(Collectors.groupingBy(
                e -> e.getDepartment(),
                Collectors.collectingAndThen(
                     Collectors.maxBy(Comparator.comparingInt(e -> e.getEmployeeId())),
                     e -> e.get().getSalary())
        ));
英文:

You can directly map the salary in collectingAndThen from Employee object

Map&lt;String, Integer&gt; retVal = employees.stream()
            .collect(Collectors.groupingBy(
                    e -&gt; e.getDepartment(),
                    Collectors.collectingAndThen(
                         Collectors.maxBy(Comparator.comparingInt(e -&gt; e.getEmployeeId())),
                         e -&gt; e.get().getSalary())
            ));

huangapple
  • 本文由 发表于 2020年10月4日 20:11:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64194436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定