我无法理解该函数如何通过Lambda传递给此方法
public class OrderUtil {
public static List runInBatches(List inputList,
Function, List> functionToRunInBatches) {
return Lists.partition(inputList, BATCH_CHUNK_SIZE).stream()
.flatMap(batch -> functionToRunInBatches.apply(batch).stream())
.collect(toList());
}
}
我看到下面的代码,我无法理解下面的lambda函数如何转换为上面的functionToRunInBatches? orderDao.getOrderForDates(…)接受三个参数(orders,startdate,enddate),但是我的函数接受一个列表并返回一个列表.这个电话如何运作良好?
我已经阅读了有关Function的教程和文档.
有人可以分解lambda如何映射到上面的Function吗?我无法想象这最终如何工作.
private List getOrderForDates(List orderNumbers,
tring startDate, String endDate){
return OrderUtil.runInBatches(orderNumbers,
orderBatch -> orderDAO.getOrderForDates(orderBatch, startDate, endDate));
}
解决方法:
编译器将lambda转换为新的Function对象.它使用lambda表达式中给出的代码覆盖apply方法.
所以这:
private List getOrderForDates(List orderNumbers, String startDate, String endDate){
return OrderUtil.runInBatches(orderNumbers, orderBatch -> orderDAO.getOrderForDates(orderBatch, startDate, endDate));
}
等效于此:
private List getOrderForDates(List orderNumbers, String startDate, String endDate){
return OrderUtil.runInBatches(orderNumbers, new Function, List>() {
@Override
public List apply(List orderBatch) {
return orderDAO.getOrderForDates(orderBatch, startDate, endDate);
}
});
}
然后,您的runInBatches方法仅将调用应用于该Function对象.
标签:java-8,java,lambda
来源: https://codeday.me/bug/20191025/1924790.html