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

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

Java functional programming: create list of object from a different list

问题

我想将以下方法转换为函数式编程:

  1. public static List<PayrollEntry> payroll(List<Employee> employees) {
  2. List<PayrollEntry> payrollEntries = new ArrayList<PayrollEntry>();
  3. for(Employee emp:employees){
  4. PayrollEntry pEntry = new PayrollEntry(emp,emp.getSalary(),new BigDecimal(1000));
  5. payrollEntries.add(pEntry);
  6. }
  7. return payrollEntries;
  8. }

有人可以向我展示如何使用函数式编程基于当前列表创建对象实例吗?

谢谢

英文:

I'd like to convert to funcional programming the following method:

  1. public static List&lt;PayrollEntry&gt; payroll(List&lt;Employee&gt; employees) {
  2. List&lt;PayrollEntry&gt; payrollEntries = new ArrayList&lt;PayrollEntry&gt;();
  3. for(Employee emp:employees){
  4. PayrollEntry pEntry = new PayrollEntry(emp,emp.getSalary(),new BigDecimal(1000));
  5. payrollEntries.add(pEntry);
  6. }
  7. return payrollEntries;
  8. }

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方法:

  1. public static List<PayrollEntry> payroll(List<Employee> employees) {
  2. return employees.stream()
  3. .map(emp -> new PayrollEntry(emp, emp.getSalary(), new BigDecimal(1000)))
  4. .collect(Collectors.toList());
  5. }

虽然最好在PayrollEntry中提供更好的拷贝构造函数:

  1. public class PayrollEntry {
  2. // ...
  3. public PayrollEntry(Employee emp) {
  4. this(emp, emp.getSalary(), new BigDecimal(1000));
  5. }
  6. // ...
  7. }

然后可以将构造函数用作方法引用:

  1. public static List<PayrollEntry> payroll(List<Employee> employees) {
  2. return employees.stream()
  3. .filter(Objects::nonNull) // 过滤掉空值以防止 NPE
  4. .map(PayrollEntry::new)
  5. .collect(Collectors.toList());
  6. }
英文:

You should just use map and collect methods of the stream:

  1. public static List&lt;PayrollEntry&gt; payroll(List&lt;Employee&gt; employees) {
  2. return employees.stream()
  3. .map(emp -&gt; new PayrollEntry(emp,emp.getSalary(),new BigDecimal(1000)))
  4. .collect(Collectors.toList());
  5. }

Though it would be better to provide better copy constructor in PayrollEntry:

  1. public class PayrollEntry {
  2. // ...
  3. public PayrollEntry(Employee emp) {
  4. this(emp, emp.getSalary(), new BigDecimal(1000));
  5. }
  6. // ...
  7. }

Then it is possible to use the constructor as a method reference:

  1. public static List&lt;PayrollEntry&gt; payroll(List&lt;Employee&gt; employees) {
  2. return employees.stream()
  3. .filter(Objects::nonNull) // filter out nulls to prevent NPE
  4. .map(PayrollEntry::new)
  5. .collect(Collectors.toList());
  6. }

答案2

得分: 0

就像这样:

  1. public static List<PayrollEntry> payroll(final List<Employee> employees) {
  2. return employees.stream()
  3. .map(emp -> new PayrollEntry(emp, emp.getSalary(), new BigDecimal(1000)))
  4. .collect(Collectors.toList());
  5. }

最好将工资逻辑提取到另一个函数中(例如用于测试):

  1. public static PayrollEntry payrollOfEmployee(final Employee employee) {
  2. return new PayrollEntry(employee, employee.getSalary(), new BigDecimal(1000));
  3. }
  4. public static List<PayrollEntry> payroll(final List<Employee> employees) {
  5. return employees.stream()
  6. .map(payrollOfEmployee)
  7. .collect(Collectors.toList());
  8. }
英文:

Like so:

  1. public static List&lt;PayrollEntry&gt; payroll(final List&lt;Employee&gt; employees) {
  2. return employees.stream()
  3. .map(emp -&gt; new PayrollEntry(emp, emp.getSalary(), new BigDecimal(1000)))
  4. .collect(Collectors.toList());
  5. }

I might be best to pull out the payroll logic into another function (e.g. for testing):

  1. public static PayrollEntry payrollOfEmployee(final Employee employee) {
  2. return new PayrollEntry(employee, employee.getSalary(), new BigDecimal(1000));
  3. }
  4. public static List&lt;PayrollEntry&gt; payroll(final List&lt;Employee&gt; employees) {
  5. return employees.stream()
  6. .map(payrollOfEmployee)
  7. .collect(Collectors.toList());
  8. }

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:

确定