英文:
Java functional programming: create list of object from a different list
问题
我想将以下方法转换为函数式编程:
public static List<PayrollEntry> payroll(List<Employee> employees) {
List<PayrollEntry> payrollEntries = new ArrayList<PayrollEntry>();
for(Employee emp:employees){
PayrollEntry pEntry = new PayrollEntry(emp,emp.getSalary(),new BigDecimal(1000));
payrollEntries.add(pEntry);
}
return payrollEntries;
}
有人可以向我展示如何使用函数式编程基于当前列表创建对象实例吗?
谢谢
英文:
I'd like to convert to funcional programming the following method:
public static List<PayrollEntry> payroll(List<Employee> employees) {
List<PayrollEntry> payrollEntries = new ArrayList<PayrollEntry>();
for(Employee emp:employees){
PayrollEntry pEntry = new PayrollEntry(emp,emp.getSalary(),new BigDecimal(1000));
payrollEntries.add(pEntry);
}
return payrollEntries;
}
Someone could show me how can I create an instance of an object based on the current list using functional programming?
Thank you
答案1
得分: 1
你应该只使用流的map
和collect
方法:
public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees.stream()
.map(emp -> new PayrollEntry(emp, emp.getSalary(), new BigDecimal(1000)))
.collect(Collectors.toList());
}
虽然最好在PayrollEntry
中提供更好的拷贝构造函数:
public class PayrollEntry {
// ...
public PayrollEntry(Employee emp) {
this(emp, emp.getSalary(), new BigDecimal(1000));
}
// ...
}
然后可以将构造函数用作方法引用:
public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees.stream()
.filter(Objects::nonNull) // 过滤掉空值以防止 NPE
.map(PayrollEntry::new)
.collect(Collectors.toList());
}
英文:
You should just use map
and collect
methods of the stream:
public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees.stream()
.map(emp -> new PayrollEntry(emp,emp.getSalary(),new BigDecimal(1000)))
.collect(Collectors.toList());
}
Though it would be better to provide better copy constructor in PayrollEntry
:
public class PayrollEntry {
// ...
public PayrollEntry(Employee emp) {
this(emp, emp.getSalary(), new BigDecimal(1000));
}
// ...
}
Then it is possible to use the constructor as a method reference:
public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees.stream()
.filter(Objects::nonNull) // filter out nulls to prevent NPE
.map(PayrollEntry::new)
.collect(Collectors.toList());
}
答案2
得分: 0
就像这样:
public static List<PayrollEntry> payroll(final List<Employee> employees) {
return employees.stream()
.map(emp -> new PayrollEntry(emp, emp.getSalary(), new BigDecimal(1000)))
.collect(Collectors.toList());
}
最好将工资逻辑提取到另一个函数中(例如用于测试):
public static PayrollEntry payrollOfEmployee(final Employee employee) {
return new PayrollEntry(employee, employee.getSalary(), new BigDecimal(1000));
}
public static List<PayrollEntry> payroll(final List<Employee> employees) {
return employees.stream()
.map(payrollOfEmployee)
.collect(Collectors.toList());
}
英文:
Like so:
public static List<PayrollEntry> payroll(final List<Employee> employees) {
return employees.stream()
.map(emp -> new PayrollEntry(emp, emp.getSalary(), new BigDecimal(1000)))
.collect(Collectors.toList());
}
I might be best to pull out the payroll logic into another function (e.g. for testing):
public static PayrollEntry payrollOfEmployee(final Employee employee) {
return new PayrollEntry(employee, employee.getSalary(), new BigDecimal(1000));
}
public static List<PayrollEntry> payroll(final List<Employee> employees) {
return employees.stream()
.map(payrollOfEmployee)
.collect(Collectors.toList());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论