Java Lambda表达式与Groovy闭包的逻辑类似吗? [英] Is Java Lambda expression is similar logic of Groovy closure?
问题描述
我学习了java 8新功能 Lambda表达式。所以这是使用Lambda表达式的我的HelloWorld类
public class LambdaHelloWorld {
interface HelloWorld {
String sayHello(String name);
}
public static void main(String [] args){
HelloWorld helloWorld =(String name) - > {returnHello+ name; };
System.out.println(helloWorld.sayHello(John Doe));
$ p这种风格与Groovy闭包非常相似。这是常规的HelloWorld def sayHello(name){
println(Hello $ name!)
}
def clos = {name - > sayHello(name)} clos.call('John Doe')
我认为这两个代码少相互之间的区别。是Java Lambda表达式是类似于Groovy闭包的逻辑还是风格?解析方案
实现一个所谓的功能接口在Java 8(使用lambda表达式)或Groovy(使用闭包)看起来完全相同,但底层机制却非常不同。我们以 java.util.function.Consumer
功能界面为例。我们用它在一个假设的 java.util.List
实例上调用新的Java 8 forEach()
code> myList 。
在Java中,它看起来像这样:
<$ ((s) - > System.out.println(s)); p $ p> myList.forEach
与Groovy相同:
myList.forEach {s - > println s}
这两个编译器都会从lambda / closure代码生成新的类。 Java 8生成的类实现了目标接口(在这种情况下为 Consumer
),而不是从任何东西派生出来,类似于嵌入式匿名类,如下所示:
myList.forEach(new Consumer< Object>(){
@Override
public void accept(Object s){
System.out.println(s);
}
});
相比之下,Groovy生成的内容有点像下面这样:
myList.forEach(new Closure(this){
void doCall(Object s){
println s
} $
这将创建一个从 groovy.lang派生的匿名类。这样做是可能的,因为Groovy在运行时生成一个动态代理对象,实现了'消费者' '接口并将任何调用转发到生成的Closure实例。
因此,您可以用Groovy闭包来替换Java 8 lambda表达式,但不能相反。想要在Java 8代码中使用Groovy API,不能调用期望带有lambda表达式的Closure的方法。 Closure
不是函数接口,而是抽象类,这可以简单地不被lambd实现一个表达。
I learn about java 8 new feature Lambda expressions. So 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?
解决方案 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.
这篇关于Java Lambda表达式与Groovy闭包的逻辑类似吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!