Reputation: 1289
first we must declare interface in other file or same file for lambda expression.But if can we do that in method declaration that will be more easier.is there 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));
}
}
Upvotes: 3
Views: 113
Reputation: 298519
There is no way to declare an interface right inside the method declaration but you can make it a nested type which 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 that there in a misunderstanding in the implication of an anonymous interface
. You can declare anonymous classes inside a method but these are not declared “on-the-fly”; they are still types created at compile-time having their own class file which isn’t smaller than a top-level type, actually, it’s the opposite. Such types have bigger class files than top-level types as they contain meta-information about the surrounding context in which they are defined.
That wouldn’t change if there was a way to declare an interface
inside the parameter list of a method; it wouldn’t be more efficient than declaring it anywhere else. But it would require special scoping rule exceptions as under normal scoping rules you couldn’t call a method which expects a parameter of a type local to the method as the caller wouldn’t know that type.
But note that the example above is what you want to achieve, practically. You can also change the declaration of MyInterface
to public
and other classes could invoke the method with a lambda expression like with the use case app.fonk(1, 3, (c, d) -> c + d)
without the need to import MyInterface
. Which is what your question seems to be about, callers don’t have to deal with the interface
specifically defined for the fonk
parameter, the compiler does.
Upvotes: 3
Reputation: 8561
You can do this
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);
}
}
Upvotes: 0
Reputation: 5133
No there is no way of doing this in java, you cannot declare an anonymous class/interface in a method deceleration.
If you are looking for a solution for that specific scenario then I would suggest:
public int fonk(
int a,
int b,
BiFunction<Integer, Integer, Integer> anonim
) {
return anonim.apply(a, b);
}
According to: BiFunction
Upvotes: 1