How to Start Working With Lambda Expressions in Java

How to Start Working With Lambda Expressions in Java

 

如何开始使用Java中的Lambda表达式

 

 

Before Lambda expressions support was added by JDK 8, I’d only used examples of them in languages like C# and C++.

Once this feature was added to Java, I started looking into them a bit closer.

The addition of lambda expressions adds syntax elements that increase the expressive power of Java. In this article, I want to focus on foundation concepts you need to be familiar with so you can start adding lambda expressions to your code today.

 

 

在JDK 8 添加了支持Lambda表达式之前,我只在C# 和C++中使用过他们的实例,将此功能添加到Java中后我开始去研究他们了。在lambda表达式中添加了语法元素之后,提升了Java的表达能力,在这篇文章里面,我想关注你需要熟悉的基础概念,你现在可以在你的代码中添加lambda表达式了。

 

 

Quick introduction

Lambda expressions take advantage of parallel process capabilities of multi-core environments as seen with the support of pipeline operations on data in the Stream API.

They are anonymous methods (methods without names) used to implement a method defined by a functional interface. It’s important to know what a functional interface is before getting your hands dirty with lambda expressions.

 

快速介绍

lambda表达式 利用了多核环境的并行处理功能,可以见得,支持在Stream API中对数据的管道传输操作。他们是匿名方法(没有名字的方法)并且用来实现由功能接口定义的一个方法。在你开始着手剩余lambda表达式之前,知道什么是功能接口是很重要的。

 

Functional interface

A functional interface is an interface that contains one and only one abstract method.If you take a look at the definition of the Java standard Runnable interface, you will notice how it falls into the definition of functional interface because it only defines one method: run().

In the code sample below, the method computeName is implicitly abstract and is the only method defined, making MyName a functional interface.

 

功能接口是一个包含一个且只有一个抽象方法的接口。如果你看一下Java规范中的Runnable接口,你将能够注意到,它是如何进入到功能接口定义中去的,因为 它仅仅定义了一个方法 :run()方法。在代码示例下,这个方法computeName() 是一个隐式抽象并且是唯一被定义的方法,这使他成为了一个功能的接口。

 

The arrow operator

Lambda expressions introduce the new arrow operator -> into Java. It divides the lambda expressions in two parts:

 

 

箭头操作符

lambda 表达式 结束了一个新的箭头操作符-> 引入Java。 它将lambda表达式划分为两个部分:

(n) -> n*n

 

The left side specifies the parameters required by the expression, which could also be empty if no parameters are required.

The right side is the lambda body which specifies the actions of the lambda expression. It might be helpful to think about this operator as “becomes.” For example, “n becomes n*n,” or “n becomes n squared.”

With functional interface and arrow operator concepts in mind, you can put together a simple lambda expression:

左侧指定表达式的参数,如果没有没有参数可以空着。 右侧是lambda 体,指定了lambda表达式的动作,将此操作符看成变数,可能会有所帮助。 例如 n 变成 n* n 或者n变成n 的平方。

使用这个功能接口以及箭头操作符的概念,可以将它和简单的lambda表达式放到一起。

 

 

 

The variables morningGreeting and eveningGreeting, lines 6 and 7 in the sample above, make a reference to MyGreeting interface and define different greeting expressions.

When writing a lambda expression, it is also possible to explicitly specify the type of the parameter in the expression like this:

 

在上面的实例的6行到7行中,变量 morning Greeting 和 eveningGreeting,引用MyGreeting 接口,并且定义不同的greeting表达式。

当写一个lambda表达式的时候,可能在表达式中明确指定参数类型,如下所示:

 

 

Block lambda expressions

So far, I have covered samples of single expression lambdas. There is another type of expression used when the code on the right side of the arrow operator contains more than one statement known as block lambdas:

lambda表达式块

到目前为止,我介绍了单表达式的lambda的样例。当代码被写在箭头操作符的右边且包含超过一个语句的时候,就可以使用另一种表达式的类型,lambda块。

 

 

 

Generic functional interfaces

A lambda expression cannot be generic. But the functional interface associated with a lambda expression can. It is possible to write one generic interface and handle different return types like this:

 

泛型化的函数接口

一个lambda表达式不能被泛型化,但是与功能接口关联了的lambda表达式能。写一个带泛型的接口,并且处理不同的返回类型,就像下边那样。

 

 

Lambda expressions as arguments

One common use of lambdas is to pass them as arguments.

They can be used in any piece of code that provides a target type. I find this exciting, as it lets me pass executable code as arguments to methods.

To pass lambda expressions as parameters, just make sure the functional interface type is compatible with the required parameter.

 

Lambda表达式作为参数

 

一个常见的使用lambda表达式的方式就是将他们作为参数传递。

他们可以在任何一块提供了目标类型的代码中使用。我觉得这个令人兴奋,它让我可以将代码作为参数传递给方法。

将lambda表达式作为参数传递,只需要确定函数接口类型与所需参数兼容即可。

 

 

These concepts will give you a good foundation to start working with lambda expressions. Take a look at your code and see where you can increase the expressive power of Java.

这些概念将会给你开始使用lambda表达式提供一个良好的基础。看一下你的代码并且在哪你可以提高Java的表达能力。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值