Is the Java Lambda expression similar to Groovy's closure logic?

I learned about java 8 a new Lambda expression function. So this is my HelloWorld class using a 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 that these two codes have less difference between themselves. Is the Java Lambda expression similar to Groovy's logic or closure style?

+5
source share
2 answers

The implementation of the so-called functional interface in Java 8 (with lambdas) or in Groovy (with closure) looks exactly the same, but the basic mechanisms are quite different. As an example, take the functional interface java.util.function.Consumer . We use it to call the new Java 8 forEach() method on a hypothetical java.util.List instance called myList .

In Java, it looks like this:

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

Same thing in Groovy:

 myList.forEach { s -> println s } 

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

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

Unlike what Groovy generates, it looks something like this:

 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 particular interface. However, it can be used here as a parameter. This is possible because Groovy creates a dynamic proxy object at runtime by implementing the Consumer interface and forwarding any calls to the generated Closure instance.

As a result, you can replace Java 8 lambdas with Groovy closure, but not vice versa. If you want to use the Groovy API in Java 8 code, you cannot call the Closure wait method with a lambda expression. Closure not a functional interface, but an abstract class, and it simply cannot be implemented using a lambda expression.

+11
source

Java lambdas are also a closure. These are the same functions at an abstract level, but in detail and depending on the exact version, Groovy can simply create special implementation classes, while Java 8 defines a complete mechanism consisting of a lambda metafile, a lambda factory, and a mechanism that includes invokedynamic for receiving lambda metaphor.

+3
source

Source: https://habr.com/ru/post/1201399/


All Articles