新特性03| 函数式接口

简介

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces.
Before Java 8, we had to create anonymous inner class objects or implement these interfaces.

(译文)函数式接口是一种仅包含一个抽象方法的接口。从Java8起,lambda表达式可以被用来实现函数式接口。一个函数式接口可以有多个default类型方法。函数式接口的例子有Runnable、ActionListener、Comparable等。

说明

函数式接口也被称为单抽象方法接口(SAM Interface),Java8之前通常以匿名内部类方式执行,现可以被隐式地转换为lambda表达式,即可用lambda表达式实行函数式接口,详情见下方用法举例。

Java8之前已有的函数式接口:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener

Java8之前新增的函数式接口:

  • java.util.function包下所有类都是Java8新增的
    • java.util.function.Predicate举例
      • 简介
        1. Predicate 接口是一个函数式接口,它接受一个输入参数 T,返回一个布尔值结果;
        2. 该接口包含多种default方法来将Predicate组合成其他复杂的逻辑(比如:与,或,非);
        3. 该接口用于测试对象是 true 或 false。
      • 代码示例
        1
        2


语法

1
2
3
4
@FunctionalInterface
interface InterfaceName{
[abstract] return_type method_name(...);
}

简单用法举例

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
public class LambdaTest {
@FunctionalInterface
interface GreetingService {
void sayMessage(String message);

default void defaultMethod() {
System.out.println("This is a default method!");
}
}

public static void main(String[] args) {
GreetingService greetingService1 = new GreetingService() {
@Override
public void sayMessage(String message) {
System.out.println("Hi " + message);
}
};
greetingService1.sayMessage("Test!");
greetingService1.defaultMethod();
System.out.println();

GreetingService greetingService2 = message -> System.out.println("Hello " + message);
greetingService2.sayMessage("World!");
greetingService2.defaultMethod();
}
}

输出:

1
2
3
4
5
Hi Test!
This is a default method!

Hello World!
This is a default method!

函数接口示例

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Java8Tester {
private static void eval(List<Integer> list, Predicate<Integer> predicate) {
// 同下方lambda表达式写法
/*for (Integer n : list) {

if (predicate.test(n)) {
System.out.println(n);
}
}*/

list.forEach(n -> {
if (predicate.test(n))
System.out.println(n);
});
}

public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// 同下方lambda表达式写法
/*Predicate<Integer> predicate = new Predicate<Integer>() {
@Override
public boolean test(Integer n) {
return true;
}
};*/

// lambda表达式写法
// Predicate<Integer> predicate = n -> true;

System.out.println("输出所有数据:");
eval(list, n -> true);
System.out.println("________________________________________");

// 同下方lambda表达式写法
/*Predicate<Integer> predicate1 = new Predicate<Integer>() {
@Override
public boolean test(Integer n) {
return n % 2 == 0;
}
};*/

// lambda表达式写法
// Predicate<Integer> predicate1 = n -> n%2 == 0;

System.out.println("输出所有偶数:");
eval(list, n -> n % 2 == 0);
System.out.println("________________________________________");

// 同下方lambda表达式写法
/*Predicate<Integer> predicate2 = new Predicate<Integer>() {
@Override
public boolean test(Integer n) {
return n > 3;
}
};*/

// lambda表达式写法
// Predicate<Integer> predicate2 = n -> n > 3;

System.out.println("输出大于 3 的所有数字:");
eval(list, n -> n > 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
输出所有数据:
1
2
3
4
5
6
7
8
9
________________________________________
输出所有偶数:
2
4
6
8
________________________________________
输出大于 3 的所有数字:
4
5
6
7
8
9

更多详细内容参见官方文档

码哥 wechat
欢迎关注个人订阅号:「码上行动GO」