Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java Lambda expression is similar logic of Groovy closure?

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

public class LambdaHelloWorld {
    interface HelloWorld {
        String sayHello(String name);
    }

    public static void main(String[] args) {          
         HelloWorld helloWorld = (String name) -> { return "Hello " + name; };
         System.out.println(helloWorld.sayHello("John Doe"));
    }
}

This style is so similar to Groovy closure. This is groovy "HelloWorld"

def sayHello(name) {
        println("Hello $name!")
}

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

I think these two codes are less difference between each others.Is Java Lambda expression is similar logic or style of Groovy closure?

like image 613
Sai Ye Yan Naing Aye Avatar asked Aug 29 '14 09:08

Sai Ye Yan Naing Aye


People also ask

Is closure the same as lambda?

A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.

Does Groovy support lambda?

groovy script does not support lambda.

How do I use lambda expression in Groovy?

The Groovy syntax doesn't support the lambda expressions, but we can rely on closure coersion to use Groovy closures as Java lambda expressions in our code. In the following sample we use the Java Streams API. Instead of lambda expressions for the filter and map methods we use Groovy closures.

What is a Groovy closure?

A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope.


1 Answers

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.

like image 174
jst Avatar answered Sep 21 '22 03:09

jst