如何仅从所需的类(流)获取数据?



Employeeclass:

public abstract class Employee extends Person {
private final Manager manager;
private final BigDecimal salary;

protected Employee(String firstName, String surname, LocalDate birth_date, Manager _manager, BigDecimal _salary) {
super(firstName, surname, birth_date);
manager = _manager;
salary = _salary;
if (manager != null) {
manager.getSubordinates().add(this);
}
}
...
}

Worker等级:

public class Worker extends Employee {
private final LocalDate employment_date;
private BigDecimal bonus;

public Worker(String firstName, String surname, LocalDate birth_date, Manager manager, BigDecimal salary,
LocalDate _employment_date, BigDecimal _bonus) {
super(firstName, surname, birth_date, manager, salary);
employment_date = _employment_date;
bonus = _bonus;
}
...
}

Managerclass:

public final class Manager extends Worker {
List<Employee> subordinates = new ArrayList<Employee>();

public Manager(String firstName, String surname, LocalDate birth_date, Manager manager, BigDecimal salary,
LocalDate employment_date, BigDecimal bonus) {
super(firstName, surname, birth_date, manager, salary, employment_date, bonus);
}
...
}

Traineeclass:

public class Trainee extends Employee {
private final LocalDate start_date;
private final short apprenticeship_length;

public Trainee(String firstName, String surname, LocalDate birth_date, Manager manager, BigDecimal salary,
LocalDate _start_date, short _apprenticeship_length) {
super(firstName, surname, birth_date, manager, salary);
manager.getSubordinates().add(this);
start_date = _start_date;
apprenticeship_length = _apprenticeship_length;
}
}

payrolclass:

public final class PayrollEntry {
private final Employee _employee;
private final BigDecimal _salaryPlusBonus;

public PayrollEntry(Employee employee, BigDecimal salary, BigDecimal bonus) {
_employee = employee;
_salaryPlusBonus = salary.add(bonus);
}
}

我必须写函数List<PayrollEntry> payroll(List<Employee> employees) {}。正如你在上面看到的,只有WorkerManager有奖励,Trainee没有,但它们都来自Employee类(顺便说一句,我不能改变类的层次结构,因为这是我的作业,层次结构是由老师写的)。我应该使用函数式编程技术来编写函数,这是我的尝试:

public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees
.stream()
.map(employee -> new PayrollEntry(employee, employee.getSalary(), ((Worker) employee).getBonus()))
.collect(Collectors.toList());
}

我明白为什么它给了我一个ClassCastException,但我不知道使用stream的任何其他方法。我想我可以用for-each循环检查每次是否为Trainee,但我想知道是否有一种使用stream的方法。

是否对map()的内容有任何限制?

,否则你可以这样写:

.map(employee -> {
if (employee instanceof Worker) {
return new PayrollEntry()....
} else {
return new PayrollEntry()....
}
})

我将为这些情况创建一个静态构造函数。(类似PayrollEntry.forEmployee(Employee))因为你不应该改变原来的类,你可以把方法放在其他地方。

private static PayrollEntry newPayrollEntry(Employee employee) {
BigDecimal bonus = BigDecimal.ZERO;        
if (employee instanceof Worker) {
bonus = ((Worker) employee).getBonus();
}
return new PayrollEntry(employee, employee.getSalary(), bonus);
}
public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees
.stream()
.map(Main::newPayrollEntry)
.collect(Collectors.toList());
}

您也可以将相同的代码放在花括号中,但是将代码移动到静态方法中更容易阅读。

stream.map(employee -> {
// Long code
return payroll;
})

我不确定它是否适合您的老师给您的任务定义,但您可以扩展您的map流操作如下:

public static List<PayrollEntry> payroll(List<Employee> employees) {
return employees
.stream()
.map(employee -> {
// if statements to check type of employee
// set some variables for the various fields
return new PayroleEntry(...);
})
.collect(Collectors.toList());
}

可以在强制转换之前使用三元操作符,检查employee是否是Worker类的实例。如果是通过奖金,则BigDecimal.ZERO:

employees.stream()
.map(employee -> new PayrollEntry(employee, employee.getSalary(),
employee instanceof Worker ? ((Worker) employee).getBonus() : BigDecimal.ZERO))
.collect(Collectors.toList());

最新更新