简体   繁体   English

将Lambda表达式转换为方法参考

[英]Conversion of Lambda expression to Method reference

I was trying to convert a lambda expression into method reference, but I failed to do so. 我试图将lambda表达式转换为方法引用,但未能这样做。 can anybody help me with this? 有人可以帮我吗? The lambda expression takes 2 int parameters adds it and return the result. lambda表达式采用2个int参数将其相加并返回结果。

public class Addition { 公共类加法{

public static void main(String[] args) {
    int a = 10;
    int b = 20;

    A ref = (int c, int d) -> c + d;
    System.out.println(ref.add(a, b));

}

} }

Based on the signature of the method implemented by your lambda expression, you can replace it with: 根据lambda表达式实现的方法的签名,可以将其替换为:

A ref = Integer::sum;

Since that sum method accepts two int arguments and returns their int sum: 由于该sum方法接受两个int参数并返回其int sum:

public static int sum(int a, int b) {
    return a + b;
}

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

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