I have been using Java 8 from last couple of months and trying to get my head around lambdas. I have a quite bit understanding about concert. But struggling with custom functional interface execution as lambda call.
If I create java Bifuctional interface implementation
BiFunction<t1,t2,R> trade = (t1, t2) -> {
// calling another method for merger
return t1,t2;
};
Can I call it as lambda just like below?
(a, b)-> trade:
Or do I have to create execute method?
private int execute(BiFunction<t1,t2,R> trade, int a, int b){
return trade.apply(a, b)
}
Here is an example of code that calls the lambda:
BiFunction<t1,t2,R> trade = (t1, t2) -> {
// calling another method for merger return t1+t2;
};
public static void main(String[] args) {
execute(trade , 1, 2);
}
private int execute(BiFunction<t1,t2,R> trade, int a, int b) {
return trade.apply(a, b);
}
I am curious why compiler can not understand this
public static void main(String[] args) { int i= (1,2) -> trade; }
If you're a long time Java coder, the new lambda stuff can be confusing -- it was(is?) for me. Lambdas are a contextual construct that the compiler interprets based on the context where it is used, just like a lot of other scripting languages. For most cases, you never need to create your own sub-types because the compiler does it for you based on where you are using them. Objectively, we think, "Yeah, I know. Duh." But it's tough making your brain change thought patterns. That's one of the big hurdles I think a lot of developers need to overcome.
I can see how we want to think, "Gee, I want my own
BiConsumer
that concatenates twoStrings
and adds them to aList
." So you generate your class file in your favorite IDE and getAnd when you go to try and find a good use for it you can't really seem to find one. That's because there isn't any need to create one.
I've been hard-pressed to find a situation where I need to create my own class that implements any of the
java.util.function
stuff. Not saying there aren't cases, I just haven't found too many.It boils down to recognizing when you have something that takes a type that has a single abstract method, like
Comparable.compare(T t1, T t2)
orRunnable.run()
. In these cases, you simply in-line the lambda construct.So using the functionality of my
StringConcat
, given aMap<String,String> map
EDIT: After your comments and re-reading your question, I think you still have a bit of a mental block. It looks as though you want to declare a lambda, instantiate it and call it in three distinct steps. That sort of defeats the purpose. When you use lambdas, you do the first two steps when you create the lambda inline with something that takes it.
This is saying, "for every string in the list, apply this function,
Integer.parseInt(string)
." Notice you don't ever callapply()
. That's done within themap
method. This avoids the need to do what you are doing with creating an instance and separately trying to call it.Lambdas are not a magic bullet. In most cases, you still need to have separate methods with distinct statements. Maybe the functionality you want doesn't play well with lambdas.