好处:使自己的代码更灵活,更复用
1. function 原方法
public Date getOk2Ship() {
return get(ProjectScheduleEntity::getOk2ship);
}
2. function 使用
相当于一个方法作为另一个方法的参数
private ProjectScheduleDateInterval signProduct(List<ProductEntity> productEntities) {
ProjectScheduleDateInterval projectScheduleDateInterval = new ProjectScheduleDateInterval(productEntities, DAY_BEFORE_POR_EXIT, DAY_AFTER_POR_EXIT);
// 可以使用方法引用
projectScheduleDateInterval.groupProductByField(ProjectSchedule::getOk2Ship);
// 也可以使用 lambda表达式
projectScheduleDateInterval.groupProductByField(o->o.getOk2Ship());
return projectScheduleDateInterval;
}
3. function 获取结果
import java.util.function.Function;
/**
* 使用 function 作为参数
* ProjectSchedule function函数第一个参数为入参类型
* Date function函数第二个参数返回参数类型
*/
public void groupProductByField(Function<ProjectSchedule, Date> function) {
productProjectSchedules.entrySet().stream().forEach(entry -> {
ProjectSchedule projectSchedule = new ProjectSchedule(entry.getValue());
Date date = function.apply(projectSchedule);
// todo
});
}