Java函数式编程:从另一个列表创建对象列表

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

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&lt;PayrollEntry&gt; payroll(List&lt;Employee&gt; employees) {
	List&lt;PayrollEntry&gt; payrollEntries = new ArrayList&lt;PayrollEntry&gt;();
	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

你应该只使用流的mapcollect方法:

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&lt;PayrollEntry&gt; payroll(List&lt;Employee&gt; employees) {
    return employees.stream()
                    .map(emp -&gt; 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&lt;PayrollEntry&gt; payroll(List&lt;Employee&gt; 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&lt;PayrollEntry&gt; payroll(final List&lt;Employee&gt; employees) {
  return employees.stream()
    .map(emp -&gt; 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&lt;PayrollEntry&gt; payroll(final List&lt;Employee&gt; employees) {
  return employees.stream()
    .map(payrollOfEmployee)
    .collect(Collectors.toList());
}

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

发表评论

匿名网友

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

确定