Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 8 function invocation

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; }
like image 410
Maddy Avatar asked Jun 18 '15 16:06

Maddy


People also ask

What is function method in Java 8?

The Function Interface is a part of the java. util. function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result.

What is lambda expressions Java 8?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What does -> mean in Java?

Basically, the -> separates the parameters (left-side) from the implementation (right side). The general syntax for using lambda expressions is. (Parameters) -> { Body } where the -> separates parameters and lambda expression body.

How many functional interfaces does Java 8 have?

In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios.


2 Answers

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));
}
like image 60
Filip Bulovic Avatar answered Oct 23 '22 14:10

Filip Bulovic


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.

like image 32
MadConan Avatar answered Oct 23 '22 13:10

MadConan