java 8 function invocation

7k views Asked by At

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; }
2

There are 2 answers

4
MadConan On BEST ANSWER

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 two Strings and adds them to a List." So you generate your class file in your favorite IDE and get

public static class StringConcat implements BiConsumer<String,String>{
    private List<String> values = new ArrayList<>();

    @Override
    public void accept(String s, String s2) {
        values.add(s,s2);
    }
}

And 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) or Runnable.run(). In these cases, you simply in-line the lambda construct.

So using the functionality of my StringConcat, given a Map<String,String> map

    // I just passed it a new BiConsumer!
    map.forEach((k,v)-> list.add(k+v));


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.

//                      declare AND instantiate
stringList.stream().map(string -> Integer.parseInt(string))...

This is saying, "for every string in the list, apply this function, Integer.parseInt(string)." Notice you don't ever call apply(). That's done within the map 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.

4
Filip Bulovic On

You use lambda like this

public static void main(String[] args) {
    BiFunction<Integer, Integer, Integer> add = (x, y) -> {
        return x + y;
    };
    System.out.println(add.apply(1, 2));
}