How can we do this in a Lambda expression?

first we have to declare the interface in another file or the same file for the lambda expression. But if we can do this in the method declaration, it will be easier. Anyway?

 public class LambdaAppJava8 {
 public int fonk(
     int a, 
     int b, 
     interface MyInterface{
          public int call(int a, int b);
     } anonim
 ) {
    return anonim.call(a, b);
 }

 public static void main(String[] args) {
    LambdaAppJava8 app = new LambdaAppJava8();
    System.out.println(app.fonk(1, 3, (c, d) -> c + d));
 }
}

      

+3


source to share


3 answers


It is not possible to declare an interface directly inside a method declaration, but you can make it a nested type that is close enough.

public class LambdaAppJava8 {
    private interface MyInterface {
        public int call(int a, int b);
    }
    public int fonk(int a, int b, MyInterface anonim) {
        return anonim.call(a, b);
    }
    public static void main(String[] args) {
        LambdaAppJava8 app = new LambdaAppJava8();
        System.out.println(app.fonk(1, 3, (c, d) -> c + d));
    }
}

      

It also seems like there is a misunderstanding about anonymous interface

. You can declare anonymous classes inside a method, but they are not declared on the fly; they are still a type created at compile time, having their own class file that is no less than the top-level type, in fact the other way around. Such types have larger class files than top-level types because they contain meta information about the surrounding context in which they are defined.



This would not change if there was a way to declare interface

inside a method parameter list; it would not be more effective than advertising it elsewhere. But this would require special scoping exceptions, as with normal scoping rules, which you could not call a method that expects a parameter of a type local to the method because the caller does not know the type.


But note that the above example is what you want to achieve in practice. You can also change the declaration MyInterface

to public

, and other classes could call the method using a lambda expression, for example, in the case of using it app.fonk(1, 3, (c, d) -> c + d)

without having to import MyInterface

. This is, in your opinion, your question, callers shouldn't have to deal with a interface

specific parameter fonk

that the compiler does.

+3


source


There is no way to do this in java, you cannot declare an anonymous class / interface when slowing down a method.

If you are looking for a solution for this particular scenario, I would suggest:



public int fonk(
     int a, 
     int b, 
     BiFunction<Integer, Integer, Integer> anonim
 ) {
    return anonim.apply(a, b);
 }

      

According to: BiFunction

+1


source


You can do it

import java.util.function.BiFunction;


public class LambdaAppJava8 {
    public static int fonk(int a, int b,int c,
             BiFunction<Integer,Integer,Integer> func
         ) {
            return func.apply(func.apply(a, b),c);
         }
    public static void main(String[] arg){
        int sum = fonk(1,2,3,(x,y)->x+y);
        int product = fonk(1,2,3,(x,y)->x*y);
        System.out.println(sum);
        System.out.println(product);
    }
}

      

0


source







All Articles