是否可能/更正(到)或使用 lambda
重写以下内容?在这里,我为 KeyMapper 和 ValueMapper 提供了内联实现 Function
.
public Map<Integer, List<Employee>> getSubordinateHighestSalEmpMapV1(List<Employee> employees) {
return employees.stream()
.filter(e -> e.getSubordinates() != null)
.collect(Collectors.toMap( //keyMapper
new Function<Employee, Integer>() {
@Override
public Integer apply(Employee t) {
return t.getId();
}
},
new Function<Employee, List<Employee>>() {//valueMapper
@Override
public List<Employee> apply(Employee t) {
List<Employee> subordinates = t.getSubordinates();
List<Employee> subOrdinatesListWithHighestSalary = new ArrayList<>();
int maxSal = Integer.MIN_VALUE;
for(Employee s: subordinates) {
if(s.getSalary() >= maxSal) {
maxSal = s.getSalary();
}
}
for(Employee s: subordinates) {
if(s.getSalary() == maxSal) {
subOrdinatesListWithHighestSalary.add(s);
}
}
return subOrdinatesListWithHighestSalary;
}
}));
}
我要达到什么目的:
员工类有 List<Employee> subordinates
.我试图在每个员工 under 的下属中获得最高薪水。每个员工可能有也可能没有下属。如果下属不存在,则他们不包括在结果中。如果有多个下属具有相同的最高薪水,则所有下属都应出现在结果中。
比如获取各部门最高薪员工(employees, if salary match)类似。
员工.java
import java.util.List;
public class Employee{
private int id;
private int salary;
private List<Employee> subordinates;
private String name;
private int age;
public int getId() {
return id;
}
public Employee setId(int id) {
this.id = id;
return this;
}
public int getSalary() {
return salary;
}
public Employee setSalary(int salary) {
this.salary = salary;
return this;
}
public List<Employee> getSubordinates() {
return subordinates;
}
public Employee setSubordinates(List<Employee> subordinates) {
this.subordinates = subordinates;
return this;
}
public String getName() {
return name;
}
public Employee setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Employee setAge(int age) {
this.age = age;
return this;
}
@Override
public String toString() {
return "Employee [id=" + id + ", salary=" + salary + ", name=" + name
+ ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id != other.id)
return false;
return true;
}
}
例如下面的输入:
- employee1(id:100) 有 employee2,employee3,employee4 其中 employee3 支付最高 30000 并且应该是产出的一部分
- employee2(id:101) 有 employee5、employee6,其中 employee5 的薪水最高,为 20000,应该属于输出
- employee3(id:102) 有 employee7 和 employee8,他们的工资都是相同的 16000 并且输出应该包含两者。
employee8(id:107) 有一名薪水为 12000 的下属 employee9,employee9 应该是输出的一部分
下面是按照上面的解释输入的:
private static List<Employee> getEmployeeListV1() { int i = 100; Employee employee1 = (Employee) new Employee().setId(i++).setSalary(10000).setAge(101).setName("emp 1"); Employee employee2 = (Employee) new Employee().setId(i++).setSalary(20000).setAge(110).setName("emp 2"); Employee employee3 = (Employee) new Employee().setId(i++).setSalary(30000).setAge(20).setName("emp 3"); Employee employee4 = (Employee) new Employee().setId(i++).setSalary(10000).setAge(32).setName("emp 4"); Employee employee5 = (Employee) new Employee().setId(i++).setSalary(20000).setAge(34).setName("emp 5"); Employee employee6 = (Employee) new Employee().setId(i++).setSalary(15000).setAge(44).setName("emp 6"); Employee employee7 = (Employee) new Employee().setId(i++).setSalary(16000).setAge(56).setName("emp 7"); Employee employee8 = (Employee) new Employee().setId(i++).setSalary(16000).setAge(65).setName("emp 8"); Employee employee9 = (Employee) new Employee().setId(i++).setSalary(12000).setAge(74).setName("emp 9"); employee1.setSubordinates(Stream.of(employee2,employee3,employee4).collect(Collectors.toList())); employee2.setSubordinates(Stream.of(employee5,employee6).collect(Collectors.toList())); employee3.setSubordinates(Stream.of(employee7,employee8).collect(Collectors.toList())); employee8.setSubordinates(Stream.of(employee9).collect(Collectors.toList())); List<Employee> employees = Stream.of(employee1,employee2, employee3,employee4,employee5, employee6,employee7,employee8, employee9).collect(Collectors.toList()); return employees; }
下面是输出:
100=[Employee [id=102, salary=30000, name=emp 3, age=20]]
101=[Employee [id=104, salary=20000, name=emp 5, age=34]]
102=[Employee [id=106, salary=16000, name=emp 7, age=56], Employee [id=107, salary=16000, name=emp 8, age=65]]
107=[Employee [id=108, salary=12000, name=emp 9, age=74]]
解释:
最佳答案
当然,您可以使用方法引用(Employee::getId
或 lambda employee -> employee.getId()
更改 keyMapper
>) 和 valueMapper
(t -> { ...
) 到 lambda,如下所示:
return employees.stream()
.filter(e -> e.getSubordinates() != null)
.collect(Collectors.toMap( //keyMapper
Employee::getId,
t -> {
List<Employee> subordinates = t.getSubordinates();
List<Employee> subOrdinatesListWithHighestSalary = new ArrayList<>();
int maxSal = Integer.MIN_VALUE;
for(Employee s: subordinates) {
if(s.getSalary() >= maxSal) {
maxSal = s.getSalary();
}
}
for(Employee s: subordinates) {
if(s.getSalary() == maxSal) {
subOrdinatesListWithHighestSalary.add(s);
}
}
return subOrdinatesListWithHighestSalary;
}));
您可以更进一步,将方法简化为:
return employees.stream()
.filter(e -> e.getSubordinates() != null)
.collect(Collectors.toMap(Employee::getId,
t -> {
int maxSal = t.getSubordinates().stream().mapToInt(Employee::getSalary).max().orElse(Integer.MIN_VALUE);
return t.getSubordinates().stream().filter(x -> x.getSalary() == maxSal).collect(toCollection(ArrayList::new));
}));
甚至更进一步:
return employees.stream()
.filter(e -> e.getSubordinates() != null)
.collect(Collectors.toMap(Employee::getId, Main::apply));
假设你有这个方法:
static List<Employee> apply(Employee t) {
List<Employee> subordinates = t.getSubordinates();
int maxSal = subordinates.stream().mapToInt(Employee::getSalary).max().orElse(Integer.MIN_VALUE);
return subordinates.stream().filter(x -> x.getSalary() == maxSal).collect(toCollection(ArrayList::new));
}
其中 Main
指的是包含 apply
辅助方法的类。
关于java - 如何使用 java lambda 重写 ValueMapper 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579713/