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

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

问题描述

lambda表达式除了保存代码行之外还有什么用处吗?

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

lambda提供了哪些特殊功能来解决难以解决的问题?我见过的典型用法是代替写这个:

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表达式中, super 具有与周围上下文相同的含义,另请参见这个答案。此外,您无法创建遮蔽周围上下文的局部变量的新局部变量。对于定义函数的预期任务,这会删除大量错误源,但它也暗示对于其他用例,可能存在匿名内部类,即使实现了功能接口,也无法将其转换为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 的引用,即如果它们访问或非 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天全站免登陆