如何使用java lambda重写ValueMapper函数
|
是否有可能/正确(to)或使用lambda重写下面的内容?在这里,我为KeyMapper和ValueMapper函数提供了内联实现.
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>下属.我想在每个员工的下属中获得最高薪水.每个员工可能有也可能没有下属.如果下属不在,则不包括在结果中.如果不止一个下属具有相同的最高薪水,则所有下属都应该出现在结果中. 例如,它类似于获得每个部门中收入最高的员工(员工,如果工资匹配). Employee.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等等 如下所述输入如下: 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,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,employee4,employee5,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,name=emp 8,age=65]] 107=[Employee [id=108,salary=12000,name=emp 9,age=74]] 阐释: 解决方法
当然,您可以使用方法引用(Employee :: getId或lambda employee – > employee.getId())和valueMapper(t – > {…)将keyMapper更改为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 helper方法的类. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
