在Java中使用lambda表达式有任何运行时好处吗? [英] Is there any runtime benefit of using lambda expression in Java?

查看:285
本文介绍了在Java中使用lambda表达式有任何运行时好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Java 8中提供的lambda表达式的博客和答案.

I am reading couple of blogs and answers as well on lambda expression provided in Java 8.

我无法弄清楚,单位lambda表达式是否对运行时有好处?

I am not able to figure out, is there any run-time benefit of unit lambda expression or not?

我从不同的来源复制了以下文本,这让我感到非常困惑.

I have copied following text from different sources which are too much confusing to me.

一个答案是说-

"lambda不会创建新范围,它们与 封闭的块/环境"

"lambdas do NOT create a new scope, they share the same scope as the enclosing block/environment"

在一个博客中-

使用lambda表达式没有运行时的好处,所以我将 请谨慎使用,因为我不介意写一些额外的内容 代码."

"There are no runtime benefits of using lambda expressions, so I will use it cautiously because I don’t mind writing few extra lines of code."

来自另一个博客-

另一个有益的lambda表达式顺序执行和并行执行 通过在方法中传递行为来提供支持"

"one more benefit lambda expressions Sequential and Parallel Execution Support by passing behavior in methods"

所以对我来说,还有很多困惑. 请帮助我清除此问题,以便在深入研究这些内容之前避免记住错误的方法.

So there is lots off confusion for me. Please help me to clear this so I can avoid keeping wrong approach in mind before digging more in to this stuff.

我已经运行了以下代码,我可以说以下代码中的lambda表达式只是对匿名内部类及其在主线程上运行的替代.

I have run following code and I can say the lambda expression in the following code is just replacement for the anonymous inner class and its running on main thread.

List<Integer> list = new ArrayList<>();
list.add(12);
list.forEach(V -> {
    System.out.println(V);
});

我们要减少时间复杂度还是空间复杂度?

推荐答案

" lambda不会创建新的作用域,它们与封闭的块/环境共享相同的作用域"是几乎正确的说法(它们确实创建了一个新的作用域,但没有创建内部类的方式),但与运行时性能没有任何关系.这与代码的正确性有关.

"lambdas do NOT create a new scope, they share the same scope as the enclosing block/ environment" is an almost correct statement (they do create a new scope, but not the way inner classes do), but doesn’t have anything to do with runtime performance. This has to do with correctness of the code.

在匿名内部类中,可以通过词法范围,在周围范围中找到匹配项或通过继承在匿名内部类的类层次结构中找到匹配项来解析标识符.在这种情况下,解析标识符的规则很复杂,很容易混淆.

Within an anonymous inner class, identifiers may get resolved through the lexical scope, finding a match in the surrounding scope, or by inheritance, finding a match in the class hierarchy of the anonymous inner class. The rules for resolving identifiers in this scenario are complex and easy to confuse.

此外,匿名类的主体创建了一个新的作用域,该作用域允许创建与周围环境的局部变量同名的变量,从而将这些变量隐藏起来.

Further, the body of an anonymous class creates a new scope that allows to create variables having the same name as local variables of the surrounding context, shadowing these variables.

相反,lambda表达式在其写入的上下文中的工作方式与其他表达式相同.它们不从将要转换为的功能接口中继承任何成员,它们无法创建新变量来掩盖现有局部变量,甚至thissuper与周围环境具有相同的含义:

In contrast, a lambda expression works like other expressions within the context they are written in. They do not inherit any members from the functional interface they will get converted to, they can not create new variables shadowing existing local variables and even this and super have the same meaning as within the surrounding context:

与出现在匿名类声明中的代码不同,lambda正文中出现的名称以及thissuper关键字的含义以及引用声明的可访问性与周围环境相同(除了lambda参数引入了新名称.

Unlike code appearing in anonymous class declarations, the meaning of names and the this and super keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).

因此,当您在同一块中包含表达式x.foo(y)() -> x.foo(y)时,对于两个表达式来说xy是否将相同的xy是显而易见的,因此,在每种情况下,它都是相同的foo方法,对于匿名内部类,您不能说那么简单,因为您必须首先分析整个内部类及其类型层次结构.

So when you have the expressions x.foo(y) and () -> x.foo(y) within the same block, it will be obvious whether x and y will be the same x and y for both expressions and hence, it will be the same foo method in each case, which you can not say that simple for anonymous inner classes, as you have to analyze the entire inner class and its type hierarchy first.

这使得lambda表达式非常适合您要定义局部函数的场景,例如将其作为参数传递给方法,甚至无需考虑实际使用的interface.除了定义功能签名之外,接口本身不会影响lambda表达式.

This makes lambda expressions ideal for scenarios where you want to define a local function and, e.g. pass it to a method as a parameter, without even thinking about the actual interface being used. The interface itself does not influence the lambda expression beyond defining the functional signature.

但这也意味着可能存在lambda表达式无法涵盖的匿名类的用例.但是lambda表达式的目的并不是要通用替代匿名内部类.

But this also implies that there might be use cases of anonymous classes that can’t be covered by lambda expressions. But the purpose of lambda expressions isn’t to be a general replacement for anonymous inner classes.

关于性能或易于并行处理, shmosel的答案已经说了.我们无法做出这样的一般性陈述,而不知道我们正在寻找哪个操作/问题以及我们实际上正在比较哪些解决方案.

When it comes to performance or easy of parallel processing, shmosel’s answer says it already. We can’t make such general statements without knowing which operation/problem we are looking at and which solutions we are actually comparing.

这篇关于在Java中使用lambda表达式有任何运行时好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆