简体   繁体   English

如何将具有多个参数的方法传递给采用List的Function?

[英]How does a method with multiple parameters get passed to a Function that takes a List?

I am not able to understand how the function gets passed via a lambda into this method 我无法理解函数如何通过lambda传递到此方法中

public class OrderUtil {
 public static <I, O> List<O> runInBatches(List<I> inputList, 
 Function<List<I>, List<O>> functionToRunInBatches) {
    return Lists.partition(inputList, BATCH_CHUNK_SIZE).stream()
            .flatMap(batch -> functionToRunInBatches.apply(batch).stream())
            .collect(toList());
    }
}

I see the below code, I am not able to understand how the lambda function below translates to functionToRunInBatches above? 我看到下面的代码,我无法理解下面的lambda函数如何转换为上面的functionToRunInBatches? orderDao.getOrderForDates(...) takes three parameters (orders, startdate, enddate) but my function takes a list and returns a list. orderDao.getOrderForDates(...)接受三个参数(orders,startdate,enddate)但我的函数接受一个列表并返回一个列表。 How does this call work fine? 这个电话如何正常工作?

I have read the tutorials and documentation on Function. 我已经阅读了关于Function的教程和文档。

Would it be possible for someone to break down how the lambda gets mapped to the Function above? 有人可以分解lambda如何映射到上面的函数吗? I am unable to visualise how this ends up working. 我无法想象这最终会如何起作用。

 private List<Order> getOrderForDates(List<Long> orderNumbers, 
                                           tring startDate, String endDate){
    return OrderUtil.runInBatches(orderNumbers,
            orderBatch -> orderDAO.getOrderForDates(orderBatch, startDate, endDate));
}

The lambda is turned into a new Function object by the compiler. 编译器将lambda转换为新的Function对象。 It overrides the apply method with the code given in the lambda expression. 它使用lambda表达式中给出的代码覆盖apply方法。

So this: 所以这:

private List<Order> getOrderForDates(List<Long> orderNumbers, String startDate, String endDate){
    return OrderUtil.runInBatches(orderNumbers, orderBatch -> orderDAO.getOrderForDates(orderBatch, startDate, endDate));
}

is equivalent to this: 相当于:

private List<Order> getOrderForDates(List<Long> orderNumbers, String startDate, String endDate){
    return OrderUtil.runInBatches(orderNumbers, new Function<List<Long>, List<Order>>() {
        @Override
        public List<Order> apply(List<Long> orderBatch) {
            return orderDAO.getOrderForDates(orderBatch, startDate, endDate);
        }
    });
}

Your runInBatches method then simply calls apply on that Function object. 然后,您的runInBatches方法只调用该Function对象的apply

This lambda expression: 这个lambda表达式:

orderBatch -> orderDAO.getOrderForDates(orderBatch, startDate, endDate)

can be targeted to the Function<List<Long>, List<Order>> functional interface because: 可以定位Function<List<Long>, List<Order>> 功能界面,因为:

  • It receives a List<Long> argument, which is named orderBatch , at the left of the -> operator 它在->运算符的左侧接收List<Long>参数,该参数名为orderBatch
  • It returns a List<Order> , because the orderDAO.getOrderForDates method returns a List<Order> 它返回List<Order> ,因为orderDAO.getOrderForDates方法返回List<Order>
  • Function is a functional interface , meaning that it declares only one abstract method, in this case apply Function是一个函数接口 ,意味着它只声明一个抽象方法,在本例中为apply

Here the number of arguments of the orderDAO.getOrderForDates method doesn't have any importance. 这里orderDAO.getOrderForDates方法的参数数量没有任何重要性。 What matters is the number and types of the arguments of the lambda expression (only one, which is orderBatch of type List<Long> ) and the number and types of the arguments of the targeted functional interface's unique abstract method , in this case the apply method of the Function interface. 重要的是lambda表达式的参数的数量和类型(只有一个,它是List<Long>类型的orderBatch )以及目标函数接口的唯一抽象方法的参数的数量和类型,在这种情况下是apply Function接口的方法。

On the other side, startDate and endDate are captured by the lambda expression and are also known as free variables , meaning that they aren't arguments of the function (they are neither arguments of the Function.apply method nor arguments of the lambda expression, as they are not declared at the left side of the -> operator). 另一方面, startDateendDate由lambda表达式捕获 ,也称为自由变量 ,这意味着它们不是函数的参数(它们既不是Function.apply方法的参数,也不是lambda表达式的参数,因为它们未在->运算符的左侧声明。 They aren't variables local to the function either, as they aren't declared inside the lambda expression. 它们也不是函数的局部变量,因为它们未在lambda表达式中声明。 In other words, they come from outside of the function without being arguments of it. 换句话说,它们来自函数之外,而不是它的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM