2022-08-11 18:10:37 +08:00
|
|
|
|
## Lambda 表达式-函数式编程
|
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
[TOC]
|
|
|
|
|
|
2022-08-14 23:46:18 +08:00
|
|
|
|
### 概述
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
|
|
|
|
+ 只有是函数式的接口才可以使用lambda表达式
|
|
|
|
|
+ 接口中有且只有一个抽象方法的接口称为函数式接口
|
|
|
|
|
+ @FunctionalInterface 注解强制检测是否为函数式接口
|
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
### 用法
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
|
|
|
|
+ 无参数,无返回值:()-> 具体实现
|
|
|
|
|
+ 有一个参数,无返回值:(x)-> 具体实现
|
|
|
|
|
+ 若只有一个参数,小括号可以省略不写:x -> 具体实现
|
|
|
|
|
+ 若有多个参数,多条语句,加个大括号就行了:(x,y)-> { 具体实现 }
|
|
|
|
|
+ 若 lambda 中只有一条语句,return 和大括号都可以省略不写,如`Comparator<Integer>com = (x,y) -> Integer.compare(x,y)`
|
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
### 四大内置函数式接口
|
|
|
|
|
|
|
|
|
|
| 接口 | 语法 |
|
|
|
|
|
| :---------------------------: | :---------------: |
|
|
|
|
|
| Consumer<T> 消费型接口 | void accept(T t) |
|
|
|
|
|
| Supplier<T> 供给型接口 | T get() |
|
|
|
|
|
| Function<T,R> 函数型接口 | R apply(T t) |
|
|
|
|
|
| Predicate<T> 断言型接口 | boolean test(T t) |
|
|
|
|
|
|
|
|
|
|
### 案例演示
|
|
|
|
|
|
|
|
|
|
#### Consumer
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
String name = "大山";
|
|
|
|
|
private void consumer(Consumer<String> consumer){
|
|
|
|
|
consumer.accept(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test1(){
|
|
|
|
|
consumer((name)-> System.out.println("我的名字:"+name));
|
|
|
|
|
// 若只有一个参数,小括号可以省略不写:x -> 具体实现
|
|
|
|
|
consumer(name-> System.out.println("他的名字:"+name));
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
我的名字:大山
|
|
|
|
|
他的名字:大山
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Supplier
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private void supplier(Supplier<Integer> supplier){
|
|
|
|
|
System.out.println("今日生产数量:"+supplier.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test2(){
|
|
|
|
|
supplier(()-> {return 123;});
|
|
|
|
|
// 若 lambda 中只有一条语句,return 和大括号都可以省略不写,如`Comparator<Integer>com = (x,y) -> Integer.compare(x,y)`
|
|
|
|
|
supplier(()-> 150);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
今日生产数量:123
|
|
|
|
|
今日生产数量:150
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Function
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private void function(String num,Function<String,Integer> function){
|
|
|
|
|
System.out.println("生产数量:"+(function.apply(num)+100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test3(){
|
|
|
|
|
function("200",num-> Integer.parseInt(num));
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
生产数量:300
|
|
|
|
|
生产数量:120
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Predicate
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private void predicate(Integer age,Predicate<Integer> predicate){
|
|
|
|
|
System.out.println("我今年成年了吗:"+predicate.test(age));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test4(){
|
|
|
|
|
predicate(15,age->age>=18);
|
|
|
|
|
predicate(23,age->age>=18);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
我今年成年了吗:false
|
|
|
|
|
我今年成年了吗:true
|
|
|
|
|
```
|
|
|
|
|
|