Java function call 8

I have been using Java 8 since the last few months and am trying to plunge into lambda. I understand a little about the concert. But the struggle with the implementation of the user functional interface in the form of a lambda call.

If I create an implementation of java Bifuctional interface

BiFunction<t1,t2,R> trade = (t1, t2) -> {
  // calling another method for merger
  return t1,t2;
};

Can it be called a lambda as shown below?

(a, b)-> trade: 

Or do I need to create an execute method?

private int execute(BiFunction<t1,t2,R> trade, int a, int b){ 
    return trade.apply(a, b)
}

Here is an example of the code that causes 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 the compiler cannot understand this.

public static void main(String[] args) { int i= (1,2) -> trade; }
+4
source share
2 answers

Java-, - (?) . Lambdas - , , , , . , , . : ", . ". . , , , .

, : "Gee, , BiConsumer Strings List". , IDE

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

, , . , .

, , java.util.function. , , .

, -, , , Comparable.compare(T t1, T t2) Runnable.run(). .

, StringConcat, Map<String,String> map

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


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

: " , Integer.parseInt(string)". , apply(). map. , , .

. . , , , .

+4

,

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

All Articles