我是靠谱客的博主 壮观小笼包,这篇文章主要介绍Java8新特性之方法引用(三),现在分享给大家,希望可以做个参考。

文章目录

    • 1. 方法引用介绍
    • 2. 方法引用的5种类型
      • 2.1 对象引用::实例方法名
      • 2.2 类名::静态方法名
      • 2.3 类名::实例方法名
      • 2.4 类名::new
      • 2.5 类名[]::new
    • 3. 相关链接

1. 方法引用介绍

方法引用其实是lambda表达式的升级写法,采用::固定语法,可以使代码更简洁、紧凑;


2. 方法引用的5种类型

2.1 对象引用::实例方法名

函数式接口抽象方法的形参列表与实例方法的一致,且方法的返回值类型一致;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class MethodReferencesTest { public static void main(String[] args) { // 对象引用 MethodReferencesTest methodReferencesTest = new MethodReferencesTest(); // lambda表达式写法 Integer r1 = MethodReferencesTest.getResult((a, b) -> methodReferencesTest.sum(a, b)); System.out.println("r1:" + r1); // 方法引用(对象引用::实例方法名)写法 Integer r2 = MethodReferencesTest.getResult(methodReferencesTest::sum); System.out.println("r2:" + r2); } public static Integer getResult(CalculateInterface calculateInterface) { return calculateInterface.calculate(123, 456); } /** * 实例方法 */ public Integer sum(Integer a, Integer b) { return a + b; } } @FunctionalInterface interface CalculateInterface { /** * 函数式接口抽象方法 */ Integer calculate(Integer a, Integer b); }
复制代码
1
2
3
4
// 运行结果 r1:579 r2:579

2.2 类名::静态方法名

函数式接口抽象方法的形参列表与静态方法的一致,且方法的返回值类型一致;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MethodReferencesTest { public static void main(String[] args) { // lambda表达式写法 Integer r1 = MethodReferencesTest.getResult((a, b) -> MethodReferencesTest.sum(a, b)); System.out.println("r1:" + r1); // 方法引用(类名::静态方法名)写法 Integer r2 = MethodReferencesTest.getResult(MethodReferencesTest::sum); System.out.println("r2:" + r2); } public static Integer getResult(CalculateInterface calculateInterface) { return calculateInterface.calculate(123, 456); } /** * 静态方法 */ public static Integer sum(Integer a, Integer b) { return a + b; } } @FunctionalInterface interface CalculateInterface { /** * 函数式接口抽象方法 */ Integer calculate(Integer a, Integer b); }
复制代码
1
2
3
4
// 运行结果 r1:579 r2:579

2.3 类名::实例方法名

函数式接口抽象方法的第一个参数是实例方法所在类的对象,剩下的参数(或无参)是实例方法的入参,且方法的返回值类型一致;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class MethodReferencesTest { public static void main(String[] args) { // lambda表达式写法 boolean r1 = MethodReferencesTest.getResult((a, b) -> a.equals(b)); System.out.println("r1:" + r1); // 方法引用(类名::实例方法名)写法 boolean r2 = MethodReferencesTest.getResult(CalculateEntity::equals); System.out.println("r2:" + r2); } public static Boolean getResult(CalculateInterface calculateInterface) { CalculateEntity a = new CalculateEntity(123); CalculateEntity b = new CalculateEntity(456); return calculateInterface.compare(a, b); } } class CalculateEntity { private Integer num; public CalculateEntity(Integer num) { this.num = num; } /** * 实例方法 */ public Boolean equals(CalculateEntity entity) { return this.num.equals(entity.num); } } @FunctionalInterface interface CalculateInterface { /** * 函数式接口抽象方法 */ Boolean compare(CalculateEntity a, CalculateEntity b); }
复制代码
1
2
3
4
// 运行结果 r1:false r2:false

2.4 类名::new

函数式接口抽象方法的形参列表与类的构造方法的一致,且抽象方法返回的类型是该类名的类型;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MethodReferencesTest { public static void main(String[] args) { // lambda表达式写法 CalculateEntity r1 = MethodReferencesTest.getResult(num -> new CalculateEntity(num)); System.out.println("r1:" + r1); // 方法引用(类名::new)写法 CalculateEntity r2 = MethodReferencesTest.getResult(CalculateEntity::new); System.out.println("r2:" + r2); } public static CalculateEntity getResult(CalculateInterface calculateInterface) { return calculateInterface.generateEntity(123); } } class CalculateEntity { private Integer num; /** * 构造方法 */ public CalculateEntity(Integer num) { this.num = num; } @Override public String toString() { return "num = " + num; } } @FunctionalInterface interface CalculateInterface { /** * 函数式接口抽象方法 */ CalculateEntity generateEntity(Integer num); }
复制代码
1
2
3
4
// 运行结果 r1:num = 123 r2:num = 123

2.5 类名[]::new

函数式接口抽象方法的参数是int类型或者Integer类型,表示数组长度,抽象方法的返回值类型是该类名类型的数组;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class MethodReferencesTest { public static void main(String[] args) { // lambda表达式写法 CalculateEntity[] r1 = MethodReferencesTest.getResult(length -> new CalculateEntity[length]); System.out.println("r1:" + Arrays.toString(r1)); // 方法引用(类名[]::new)写法 CalculateEntity[] r2 = MethodReferencesTest.getResult(CalculateEntity[]::new); System.out.println("r2:" + Arrays.toString(r2)); } public static CalculateEntity[] getResult(CalculateInterface calculateInterface) { return calculateInterface.generateEntityArr(5); } } class CalculateEntity { private int num; @Override public String toString() { return "num = " + num; } } @FunctionalInterface interface CalculateInterface { /** * 函数式接口抽象方法 */ CalculateEntity[] generateEntityArr(Integer length); }
复制代码
1
2
3
4
// 运行结果 r1:[null, null, null, null, null] r2:[null, null, null, null, null]

3. 相关链接

Java8新特性之Lambda表达式(一)

Java8新特性之函数式接口(二)

Java8新特性之方法引用(三)

Java8新特性之接口的默认方法和静态方法(四)

Java8新特性之重复注解和类型注解(五)

Java8新特性之Stream流(六)

Java8新特性之Optional容器(七)

Java8新特性之新的日期时间类(八)

最后

以上就是壮观小笼包最近收集整理的关于Java8新特性之方法引用(三)的全部内容,更多相关Java8新特性之方法引用(三)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部