除了保存代码行之外,lambda 表达式还有其他用途吗? [英] Do lambda expressions have any use other than saving lines of code?

查看:29
本文介绍了除了保存代码行之外,lambda 表达式还有其他用途吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lambda 表达式除了节省代码行之外还有其他用途吗?

Do lambda expressions have any use other than saving lines of code?

lambdas 是否提供了任何特殊功能来解决不容易解决的问题?我见过的典型用法是,而不是这样写:

Are there any special features provided by lambdas which solved problems which weren't easy to solve? The typical usage I've seen is that instead of writing this:

Comparator<Developer> byName = new Comparator<Developer>() {
  @Override
  public int compare(Developer o1, Developer o2) {
    return o1.getName().compareTo(o2.getName());
  }
};

我们可以使用 lambda 表达式来缩短代码:

We can use a lambda expression to shorten the code:

Comparator<Developer> byName =
(Developer o1, Developer o2) -> o1.getName().compareTo(o2.getName());

推荐答案

Lambda 表达式通常不会改变您可以使用 Java 解决的问题集,但肯定会使解决某些问题更容易,这与我们正在解决的问题相同不再用汇编语言编程了.从程序员的工作中删除多余的任务使生活更轻松,并允许做一些你甚至不会接触的事情,只是为了你必须(手动)生成的代码量.

Lambda expressions do not change the set of problems you can solve with Java in general, but definitely make solving certain problems easier, just for the same reason we’re not programming in assembly language anymore. Removing redundant tasks from the programmer’s work makes life easier and allows to do things you wouldn’t even touch otherwise, just for the amount of code you would have to produce (manually).

但是 lambda 表达式不仅仅是节省代码行.Lambda 表达式允许您定义函数,在此之前您可以使用匿名内部类作为解决方法,这就是为什么您可以在这些情况下替换匿名内部类,但通常不能.

But lambda expressions are not just saving lines of code. Lambda expressions allow you to define functions, something for which you could use anonymous inner classes as a workaround before, that’s why you can replace anonymous inner classes in these cases, but not in general.

最值得注意的是,lambda 表达式是独立于它们将转换为的函数接口定义的,因此没有它们可以访问的继承成员,此外,它们无法访问实现函数接口的类型的实例.在 lambda 表达式中,thissuper 与周围上下文中的含义相同,另请参见 这个答案.此外,您不能创建新的局部变量来遮蔽周围上下文的局部变量.对于定义函数的预期任务,这消除了很多错误源,但这也意味着对于其他用例,可能存在无法转换为 lambda 表达式的匿名内部类,即使实现了函数接口.

Most notably, lambda expressions are defined independently to the functional interface they will be converted to, so there are no inherited members they could access, further, they can not access the instance of the type implementing the functional interface. Within a lambda expression, this and super have the same meaning as in the surrounding context, see also this answer. Also, you can not create new local variables shadowing local variables of the surrounding context. For the intended task of defining a function, this removes a lot of error sources, but it also implies that for other use cases, there might be anonymous inner classes which can not be converted to a lambda expression, even if implementing a functional interface.

此外,结构 new Type() { ... } 保证产生一个新的不同实例(正如 new 总是做的那样).如果在非static 上下文中创建,匿名内部类实例始终保留对其外部实例的引用.相比之下,lambda 表达式仅在需要时捕获对 this 的引用,即如果它们访问 this 或非 static 成员.并且它们生成有意未指定身份的实例,这允许实现在运行时决定是否重用现有实例(另请参见做 lambda表达式每次执行时都会在堆上创建一个对象吗?").

Further, the construct new Type() { … } guarantees to produce a new distinct instance (as new always does). Anonymous inner class instances always keep a reference to their outer instance if created in a non-static context. In contrast, lambda expressions only capture a reference to this when needed, i.e. if they access this or a non-static member. And they produce instances of an intentionally unspecified identity, which allows the implementation to decide at runtime whether to reuse existing instances (see also "Does a lambda expression create an object on the heap every time it's executed?").

这些差异适用于您的示例.您的匿名内部类构造将始终生成一个新实例,它也可能捕获对外部实例的引用,而您的 (Developer o1, Developer o2) ->o1.getName().compareTo(o2.getName()) 是一个非捕获的 lambda 表达式,在典型的实现中将评估为单例.此外,它不会在您的硬盘驱动器上生成 .class 文件.

These differences apply to your example. Your anonymous inner class construct will always produce a new instance, also it may capture a reference to the outer instance, whereas your (Developer o1, Developer o2) -> o1.getName().compareTo(o2.getName()) is a non-capturing lambda expression that will evaluate to a singleton in typical implementations. Further, it doesn’t produce a .class file on your hard drive.

鉴于语义和性能方面的差异,Lambda 表达式可能会改变程序员在未来解决某些问题的方式,当然,这也是由于新 API 包含利用新语言特性的函数式编程思想.另请参阅Java 8 lambda 表达式和一级值.

Given the differences regarding both, semantic and performance, lambda expressions may change the way programmers will solve certain problems in the future, of course, also due to the new APIs embracing ideas of functional programming utilizing the new language features. See also Java 8 lambda expression and first-class values.

这篇关于除了保存代码行之外,lambda 表达式还有其他用途吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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