函数式接口介绍:

函数式接口是Java 接口的一种,但在接口的基础上,还需要满足:

  1. 一个函数式接口只有一个抽象方法(SAM,single abstract method);
  2. Object 类中的 public abstract method 不会被视为单一的抽象方法;
  3. 函数式接口可以有默认方法和静态方法;
  4. 函数式接口可以用@FunctionalInterface 注解进行修饰。

函数式接口的作用:函数式接口带给我们最大的好处就是:可以使用极简的Lambda表达式实例化接口。

四个基本的函数式接口:

image-20240515215716322

以上的函数式接口都在java.util.function包中,通常函数接口出现的地方都可以使用Lambda表达式

@FunctionalInterface :

一个接口只要满足只有一个抽象方法的条件,即可以当成函数式接口使用,有无@FunctionalInterface都无所谓,但是jdk定义了这个注解肯定是有原因的。

@FunctionalInterface更相当于是一种限制,凡是使用了这个注解的接口,开发者可放心大胆的使用lambda来实例化,使用了这个注解的接口只允许存在一个抽象方法。

Function接口

  • compose(Function before):同理,将两个 Function 组合,将先执行 compose 函数,再执行当前函数,并将 compose 函数的结果作为参数传递给当前函数。
  • identity():返回一个执行恒等转换的函数,即返回输入参数本身。

apply方法

R apply(T,t) 根据类型T的参数获取类型为R的结果

1
2
3
4
5
6
7
// Function 接口的泛型,第一个参数是入参类型,第二个参数是出参类型
// Function 接口只有一个抽象方法,就是 apply()
//利用 Lambda 表达式实现这个抽象方法并创建 Function 对象
Function<Integer, String> function = num -> "GTA" + num;
// 将5这个参数传递给function,得到返回结果
String result = function.apply(5);
System.out.println(result); // 打印:GTA5

andThen方法

1
2
3
4
5
6
7
8
9
10
11
/**
* 需求:
* 把String类型的"123",转换成Integer类型,把转换后的结果+10
* 把增加之后的Integer类型的数据转换成String类型
*/
public static void main(String[] args) {
Function<String, Integer> fun1 = str -> Integer.parseInt(str) + 10;
Function<Integer, String> fun2 = num -> num +"";
String ans = fun1.andThen(fun2).apply("123");
System.out.println(ans);
}

img

运行结果:

image-20240515220120761

compose方法

1
2
3
4
5
6
public static void main(String[] args) {
Function<String, Integer> fun1 = str -> Integer.parseInt(str) + 10;
Function<Integer, String> fun2 = num -> num +"1";
Integer ans2 = fun1.compose(fun2).apply(123);
System.out.println(ans2);
}

image-20240515220154939

运行结果:

image-20240515220216435

identify方法

1
2
Function<String, String> identity = Function.identity();
String result = identity.apply("hello"); // 打印:hello

Consumer接口

consumer意为消费者,顾名思义就是入参消费了,并不会返回结果

Consumer 接口包含两个方法:

  • accept(T t):该方法接受一个参数并执行一些操作。
  • andThen(Consumer after):同理,将两个 Consumer 组合,先后进行消费。

accept 方法

1
2
3
4
// Consumer 接口,泛型参数是入参类型,
//接受一个参数,并不返回结果,相当于消费了这个参数
Consumer<String> consumer = s -> System.out.println(s);
consumer.accept("我输入什么就打印什么"); // 打印:我输入什么就打印什么

andThen( )方法通上

Supplier 接口

Supplier即生产者,没有索取,只有付出,也就是不需要参数,但会提供一个get( )方法返回一个玩意儿给你。

Supplier接口通常用于生成一个值

get方法

1
2
3
4
5
// Supplier 接口,泛型参数是出参类型,不接受参数,
//但是会提供结果,相当于生产了某个东西
Supplier<String> supplier = () -> "提供一个我随便打的字符串给调用方";
String text = supplier.get();
System.out.println(text); // 打印:提供一个我随便打的字符串给调用方

Predicate 接口

Predicate 这个单词的意思就有「预言,预测,谓语,谓词」的意思,就是用来预测判断的。

Predicate 接口包含四个方法:
test(T t):该方法接受一个参数并返回一个布尔值
and(Predicate other):与另一个 Predicate 进行组合,实现逻辑与操作。
negate():与另一个 Predicate 进行组合,实现逻辑非操作。
or(Predicate other):与另一个 Predicate 进行组合,实现逻辑或操作。

test方法

Predicate 接口通常用于测试一个条件是否成立

1
2
3
4
5
6
7
8
public class PredictTest {
public static void main(String[] args) {
//Predicate 接口,泛型参数是入参类型,返回布尔值
Predicate<String> p1 = s -> s.contains("zcy");
boolean test = p1.test("zcyyyds");
System.out.println(test);
}
}

运行结果:true

and方法

逻辑与(全真则真)

1
2
3
4
5
6
7
8
9
10
11
12
13
public class PredictTest {
public static void main(String[] args) {
//Predicate 接口,泛型参数是入参类型,返回布尔值
Predicate<String> p1 = s -> s.contains("zcy");
boolean test = p1.test("zcyyyds");
System.out.println(test);//true

Predicate<String> p2 = s -> s.endsWith("yyds");
Predicate<String> p3 = p1.and(p2);
System.out.println(p3.test("zcyyyds"));//true
System.out.println(p3.test("zcyyyds?"));//false
}
}

negate方法

逻辑非(原本值反值)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class PredictTest {
public static void main(String[] args) {
//Predicate 接口,泛型参数是入参类型,返回布尔值
Predicate<String> p1 = s -> s.contains("zcy");
boolean test = p1.test("zcyyyds");
System.out.println(test);//true

Predicate<String> p2 = s -> s.endsWith("yyds");
Predicate<String> p3 = p1.and(p2);
System.out.println(p3.test("zcyyyds"));//true
System.out.println(p3.test("zcyyyds?"));/false
System.out.println(p3.negate().test("zcyyyds?"));//true
}
}

or 方法

逻辑或(有1则1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class PredictTest {
public static void main(String[] args) {
//Predicate 接口,泛型参数是入参类型,返回布尔值
Predicate<String> p1 = s -> s.contains("zcy");
boolean test = p1.test("zcyyyds");
System.out.println(test);//true

Predicate<String> p2 = s -> s.endsWith("yyds");
Predicate<String> p3 = p1.and(p2);
System.out.println(p3.test("zcyyyds"));//true
System.out.println(p3.test("zcyyyds?"));//false
System.out.println(p3.negate().test("zcyyyds?"));//true

Predicate<String> p4 = p1.or(p2);
System.out.println(p4.test("zcyyyds?"));//true
}
}