Is Java Lambda expression is similar logic of Groovy closure?

前端 未结 3 1752
旧时难觅i
旧时难觅i 2021-02-05 19:06

I\'m learning about Java 8 new feature Lambda expressions. This is my "HelloWorld" class using Lambda expression

public class LambdaHel         


        
相关标签:
3条回答
  • 2021-02-05 19:18

    I wanted a simple local closure to pass / execute. I never found this answer anywhere, so adding my spin, so you can see actual mapping from local groovy closure to local java lambda.

    Groovy:

    def clos = {name -> sayHello(name)} 
    clos('John Doe')
    

    Java:

    Consumer<String> clos = name -> sayHello(name);
    clos.accept("John Doe");
    

    The key being that bloody Consumer interface with accept() method to invoke - you must match the params and return type of your Java lambda based on their pre-canned Function interfaces (java.util.function.*). That bit is a drag, compared to untyped Groovy version.

    0 讨论(0)
  • 2021-02-05 19:29

    Implementing a so-called functional interface in Java 8 (with lambdas) or in Groovy (with closures) looks quite the same, but underlying mechanisms are pretty different. Let's take the java.util.function.Consumer functional interface as an example. We use it to call the new Java 8 forEach() method on a hypothetic java.util.List instance called myList.

    In Java it looks like this:

    myList.forEach ((s) -> System.out.println(s));
    

    The same in Groovy:

    myList.forEach { s -> println s }
    

    Both compilers generate new Classes from the lambda / closure code. The class generated by Java 8 implements the target interface (Consumer in this case), not derived from anything, similar to an embedded anonymous class like this:

    myList.forEach(new Consumer<Object>() {
        @Override
        public void accept (Object s) {
            System.out.println(s);
        }
    });
    

    In contrast, what Groovy generates looks a little bit like the following:

    myList.forEach (new Closure(this) {
        void doCall(Object s) {
            println s
        }
    }
    

    This creates an anonymous class derived from groovy.lang.Closure that does not implement any specific interface. Nevertheless, it can be used as parameter here. This is possible because Groovy generates a dynamic proxy object at runtime, implementing the ´Consumer´ interface and forwarding any calls to the generated Closure instance.

    As a consequence, you can replace Java 8 lambdas by Groovy closures, but not the other way round. When you want to use a Groovy API in Java 8 code, you cannot call a method expecting a Closure with a lambda expression. Closure isn't a functional interface but an abstract class, and that can simply not be implemented by a lambda expression.

    0 讨论(0)
  • 2021-02-05 19:37

    Java's lambdas are closures as well. These are the same feature on an abstract level, but in detail and depending on exact version, Groovy may just be creating ad-hoc implementing classes, whereas Java 8 specifies a complete mechanism, consisting of lambda Metafactory, lambda factory, and a mechanism involving invokedynamic to acquire the lambda Metafactory.

    0 讨论(0)
提交回复
热议问题