如何仅从所需的 类 (流)获取数据?
How to get data only from desired classes (stream)?
Employee
class:
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
class:
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;
}
...
}
Manager
class:
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);
}
...
}
Trainee
class:
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;
}
}
payrol
class:
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) {}
。正如你在上面看到的,只有 Worker
和 Manager
可以有奖金,另一方面 Trainee
没有,但他们都来自 class Employee
(顺便说一句,我无法更改 classes 层次结构中的任何内容,因为这是我的作业,层次结构是由老师编写的)。我应该使用函数式编程技术来编写函数,这是我的尝试:
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()....
}
})
我不确定它是否符合你老师给你的任务定义,但你可以按如下方式扩展你的 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());
}
您可以在转换之前使用三元运算符并检查员工是否是 Worker
class 的实例。如果是,则通过奖金 else BigDecimal.ZERO
:
employees.stream()
.map(employee -> new PayrollEntry(employee, employee.getSalary(),
employee instanceof Worker ? ((Worker) employee).getBonus() : BigDecimal.ZERO))
.collect(Collectors.toList());
我会为这些情况创建一个静态构造函数。 (类似于 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;
})
Employee
class:
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
class:
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;
}
...
}
Manager
class:
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);
}
...
}
Trainee
class:
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;
}
}
payrol
class:
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) {}
。正如你在上面看到的,只有 Worker
和 Manager
可以有奖金,另一方面 Trainee
没有,但他们都来自 class Employee
(顺便说一句,我无法更改 classes 层次结构中的任何内容,因为这是我的作业,层次结构是由老师编写的)。我应该使用函数式编程技术来编写函数,这是我的尝试:
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()....
}
})
我不确定它是否符合你老师给你的任务定义,但你可以按如下方式扩展你的 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());
}
您可以在转换之前使用三元运算符并检查员工是否是 Worker
class 的实例。如果是,则通过奖金 else BigDecimal.ZERO
:
employees.stream()
.map(employee -> new PayrollEntry(employee, employee.getSalary(),
employee instanceof Worker ? ((Worker) employee).getBonus() : BigDecimal.ZERO))
.collect(Collectors.toList());
我会为这些情况创建一个静态构造函数。 (类似于 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;
})