Talk about the use of JAVA in Lambda expressions

A, cited in Example
I see an earlier interview questions Ali, topics are as follows: The code int [] into Integer []. I think most entry-level junior partner first thought a solution would be new Integer [] and then traverse int [] values stuffed Integer [] to achieve conversion. Code is as follows:
Here Insert Picture Description
This method is also understandable, always achieving such conversion. But if you write the above code in the interview, it may not executing the program, you will over the interview. The fact that we continued beating and hanging being constantly updated technology, but we have a saying "Those who die my dry, eventually this will make me stronger," Ado, directly on the code as follows:
Here Insert Picture Description
Is not it is amazing, I actually use a line of code to get. The above code snippet is using the Lambda expressions rewrite, simple beauty is simply beyond words. So how specific Lambda is a long list of code instead of the above then the for loop it? What is its mechanism? Do not worry, listen to me explain!

二、什么是Lambda表达式
Lambda表达式是Java SE 8中一个重要的新特性。lambda表达式允许你通过表达式来代替功能接口,允许使用更简洁的代码来创建只有一个抽象方法的接口的实例。笔者认为,这就是一种创建匿名实例的语法,这类语法要比匿名内部类创建实例时还要简洁。
Lambda 表达式的目标类型必须是"函数式接口( functional interface ) 。函数式接口代表只包含一个抽象方法的接口 。函数式接 口可以包含多个默认方法、类方法,但只能声明一个抽象方法
如果采用匿名内部类语法来创建函数式接口的实例,则只需要实现 个抽象方法,在这种情况下即可采用 Lambda 表达式来创建对象,该表达式创建出来的对象的目标类型就是这个函数式接口 查询 Java 8的API 文档,可以发现大 的函数式接口,例如: Runnable ActionListener 等接口都是函数式接口。
由于 Lambda 表达式的结果就是被当成对象 因此程序中完全可以使用 ambda 表达式进行赋值,
例如如下代码。
Here Insert Picture Description
Runnable是Java 本身提供的一个函数式接口,它只有一个无参的构造方法,因此可以用Lambda表达式创建Runnable的一个对象。

三、方法引用与构造器引用
方法引用和构造器引用可以让 Lambda 表达式的代码块更加简洁 方法引用和构造器引用都需要使用两个英文冒号 Lambda 表达式支持如下表 所示的几种引用方式:
Here Insert Picture Description
1.引用类方法
先看第 种方法引用:引用类方法 例如,定义了如下函数式接口:
Here Insert Picture Description
该函数式接口中包含 convertO抽象方法,该方法负责将 String 参数转换为Integer 下面代码使用Lambda 表达式来创建一个 Converter 对象,并调用 converter 对象的 convert()方法将字符串转换为整数了,例如如下代码:
Here Insert Picture Description
上述通过Lambda表达式创建的convert对象,也可通过引用类方法来代替,代码如下:
Here Insert Picture Description
2. 引用特定对象的实例方法
先使用 Lambda 表达式来创建 Converter对象,代码如下:
Here Insert Picture Description
上面 Lambda 表达式的代码块只有 行调用 “xiebo” indexOf()实例方法的代码 因此可 使用如方法 用进行替换:
Here Insert Picture Description
3. 引用某类对象的实例方法
下面看第三种方法引用 :引 用某类对象的实例方法。 例如, 定义了如下函数式接口:
Here Insert Picture Description
该函数式接口中包含 test()的抽象方法,该方法负责根据 String、int、 int三 个参数生 成String返回值。下面代码使用 Lambda 表达式来创建一个MyTest 对象并执行test()方法。
Here Insert Picture Description
上面 Lambda 表达式的 码块只有 a.substring(b,c),因此可以使用如下方法引用进行替换。
Here Insert Picture Description
4. 引用构造器
See for example the following references constructor, interface function is defined as follows:
Here Insert Picture Description
The following object is used to create a Constractor Lambda expressions.
Here Insert Picture Description
Lambda expressions above code block new JFrame (title) may be used to replace a configuration Cite:
Here Insert Picture Description
the same class in Java Arrays also provide examples of the many functions the interface interface, such as Comparator, XxxOperator, XxxFunction, these interfaces is a function interface, so you can use Lambda expressions to invoke the Arrays methods, there is little interest in the partnership can try it yourself.

Published an original article · won praise 0 · Views 18

Guess you like

Origin blog.csdn.net/xifengchuihaian/article/details/104606688