在Java8中,如何将参数作为参数传递lambda表达式?

在Java8中,如何将参数作为参数传递lambda表达式?,lambda,java-8,functional-interface,Lambda,Java 8,Functional Interface,这是我试过的。它甚至不编译 public class LambdaExample { public static Integer handleOperation(Integer x, Integer y, Function converter){ return converter.apply(x,y); } public static void main(String[] args){ handleOperation

这是我试过的。它甚至不编译

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  Function converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation(10,10, Operation::add);
    }

}

class Operation {

    public int add(Integer x, Integer y){
        return x+y;
    }

}
我想在这里学到的两件事是:

1) 如何将
lambda表达式
作为方法参数传递(在上面的
main
方法中)


2) 如何将参数传递给函数(在
handleOpertion
方法中,存在
编译
应用的错误只需要一个参数)

您的
handleOperation
方法需要一个实现
函数的对象,
操作::add
(方法引用)不符合条件。另外,对于两个参数,您需要使用
BiFunction

下面是一个应该有效的示例:

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  BiFunction<Integer, Integer, Integer> converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation( 10,10, new Operation() ); // should return 20
    }

}

class Operation implements BiFunction<Integer, Integer, Integer> {

    public Integer apply(Integer x, Integer y){
        return x+y;
    }

}
public类LambdaExample{
公共静态整数操作(整数x、整数y、双功能转换器){
返回转换器。应用(x,y);
}
公共静态void main(字符串[]args){
handleOperation(10,10,new Operation());//应返回20
}
}
类操作实现了双功能{
公共整数应用(整数x,整数y){
返回x+y;
}
}
更新:

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  BiFunction<Integer, Integer, Integer> converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation( 10,10, Operation::add ); // should return 20
    }

}

class Operation {

    public static int add(Integer x, Integer y){
        return x+y;
    }

}
public类LambdaExample{
公共静态整数操作(整数x、整数y、双功能转换器){
返回转换器。应用(x,y);
}
公共静态void main(字符串[]args){
handleOperation(10,10,Operation::add);//应返回20
}
}
班级作业{
公共静态整数相加(整数x,整数y){
返回x+y;
}
}

A
函数
获取输入x并产生结果y。因此,在执行
返回转换器时,您不是在寻找
函数
(没有提到您使用了原始类型)
,但对于
双功能
或更简单的函数,则使用
二进制运算符
,因为每个类型参数都是相同的

1) 如何将lambda表达式作为方法参数传递(在main方法中 (上图)

通过提供遵守
二进制运算符
接口约定的lambda表达式,即采用两个
整数
作为参数并返回一个
整数
的方法

handleOperation(10,10, (a, b) -> a + b)
2) 如何将参数传递给函数(在HandlePerition方法中, 存在编译错误,apply仅接受一个参数)

由于函数的形式为
f=>u
,因此apply方法采用单个参数并产生单个结果,就像数学函数一样,如
f(x)=2*x
(参考答案的第一部分)

这是我试过的。它甚至不编译

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  Function converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation(10,10, Operation::add);
    }

}

class Operation {

    public int add(Integer x, Integer y){
        return x+y;
    }

}
要使代码可编译,您可以在使用方法引用之前使该方法成为静态的或创建一个新实例。当
handleOperation
调用函数的apply方法时,它将引用新实例的
add
方法

handleOperation(10,10, new Operation()::add);

注意,这个方法已经存在于JDK中,它是
Integer::sum
。它采用两个基本的int值,而不是
整数
引用,但它足够接近,因此自动装箱机制将使此方法有效,在方法上下文中显示为
二进制运算符。

您的函数参数是原始的(非类型化),应该是双函数

试试这个:

public static Integer handleOperation(Integer x, Integer y,  BiFunction<Integer, Integer, Integer> converter){
    return converter.apply(x,y);
}
事实上,您已经实施了一项缩减。这一呼吁同样可以写成:

int sum = Stream.of(1, 2).reduce((x, y) -> x + y);

不,这不是我想要的。。我想在lambda中传递“add”函数。我不想通过那里的
新操作()
。谢谢。我根据我们的产品代码制作了这个示例。在那里我看到了
handleRequest(getToken(),(token)->service.makeRequest(token,ID))
。在
handleRequest(字符串令牌,函数){return Function.apply(令牌)}
中。我在想是否可以将其转换为
service::makeRequest
。但是我想这是不可能的。@brainstorm不,我不认为,因为
ID
参数。但是兰姆达很好,我想。
int sum = Stream.of(1, 2).reduce((x, y) -> x + y);