Lambda小课堂:巧用“Function”让代码优雅起来~

在开发过程中经常会使用if…else…进行判断抛出异常、分支处理等操作。这些if…else…充斥在代码中严重影响了代码代码的美观,这时我们可以利用Java 8的Function接口来消灭if…else…

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


if (...){

throw new RuntimeException("出现异常了");

}



if (...){

doSomething();

} else {

doOther();

}












Function 函数式接口

使用注解@FunctionalInterface标识,并且只包含一个抽象方法的接口是函数式接口。函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数。
Function可以看作转换型函数

Supplier供给型函数

Supplier的表现形式为不接受参数、只返回数据

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


/\*\*

\* Represents a supplier of results.

\*

\* <p>There is no requirement that a new or distinct result be returned each

\* time the supplier is invoked.

\*

\* <p>This is a <a href="package-summary.html">functional interface</a>

\* whose functional method is {@link #get()}.

\*

\* @param <T> the type of results supplied by this supplier

\*

\* @since 1.8

\*/

@FunctionalInterface

public interface Supplier<T\> {



/\*\*

\* Gets a result.

\*

\* @return a result

\*/

T get();

}














Function函数

表现形式为接收一个参数,并返回一个值。Supplier、Consumer和Runnable可以看作Function的一种特殊表现形式

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85


/\*\*

\* Represents a function that accepts one argument and produces a result.

\*

\* <p>This is a <a href="package-summary.html">functional interface</a>

\* whose functional method is {@link #apply(Object)}.

\*

\* @param <T> the type of the input to the function

\* @param <R> the type of the result of the function

\*

\* @since 1.8

\*/

@FunctionalInterface

public interface Function<T, R\> {



/\*\*

\* Applies this function to the given argument.

\*

\* @param t the function argument

\* @return the function result

\*/

R apply(T t);



/\*\*

\* Returns a composed function that first applies the {@code before}

\* function to its input, and then applies this function to the result.

\* If evaluation of either function throws an exception, it is relayed to

\* the caller of the composed function.

\*

\* @param <V> the type of input to the {@code before} function, and to the

\* composed function

\* @param before the function to apply before this function is applied

\* @return a composed function that first applies the {@code before}

\* function and then applies this function

\* @throws NullPointerException if before is null

\*

\* @see #andThen(Function)












Consumer消费型函数

Consumer消费型函数和Supplier刚好相反。Consumer接收一个参数,没有返回值

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


/\*\*

\* 表示接受单个输入参数但不返回任何结果的操作。

\* 与大多数其他功能接口不同, Consumer 它预计会通过副作用进行操作。

\* 这是一个功能接口,其 功能 方法是 accept(Object)

\*/

@FunctionalInterface

public interface Consumer<T\> {



/\*\*

\* 对给定参数执行此操作。

\* 形参: t – 输入参数

\*/

void accept(T t);



/\*\*

\* 返回一个组合,该组合 Consumer 按顺序执行此操作,后跟 after 操作。

\* 如果执行任一操作引发异常,则会将其中继到组合操作的调用方。

\* 如果执行此操作引发异常,则不会执行该 after 操作。

\* 形参: after – 此操作后要执行的操作

\* 返回值: 按 Consumer 顺序执行此操作后跟 after 操作的组合

\* 抛出: NullPointerException – 如果 after 为空

\*/

default Consumer<T\> andThen(Consumer<? super T\> after) {

Objects.requireNonNull(after);

return (T t) -> { accept(t); after.accept(t); };

}

}












Runnable无参无返回型函数

Runnable的表现形式为即没有参数也没有返回值

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


@FunctionalInterface

public interface Runnable {

/\*\*

\* When an object implementing interface <code>Runnable</code> is used

\* to create a thread, starting the thread causes the object's

\* <code>run</code> method to be called in that separately executing

\* thread.

\* <p>

\* The general contract of the method <code>run</code> is that it may

\* take any action whatsoever.

\*

\* @see java.lang.Thread#run()

\*/

public abstract void run();

}












使用小技巧

处理抛出异常的if

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
30
31
32
33
34
35
36
37
38
39
40
41


/\*\*

\* 抛异常接口

\*\*/

@FunctionalInterface

public interface ThrowExceptionFunction {



/\*\*

\* 抛出异常信息

\*

\* @param message 异常信息

\* @return void

\*\*/

void throwMessage(String message);

}












2编写判断方法 创建工具类VUtils并创建一个isTure方法,方法的返回值为刚才定义的函数式接口-ThrowExceptionFunction。ThrowExceptionFunction的接口实现逻辑为当参数b为true时抛出异常

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


/\*\*

\* 如果参数为true抛出异常

\*

\* @param b

\* @return com.example.demo.func.ThrowExceptionFunction

\*\*/

public static ThrowExceptionFunction isTure(boolean b){



return (errorMessage) -> {

if (b){

throw new RuntimeException(errorMessage);

}

};

}












3使用方式 调用工具类参数参数后,调用函数式接口的throwMessage方法传入异常信息。 当出入的参数为false时正常执行

处理if分支操作

1定义函数式接口 创建一个名为BranchHandle的函数式接口,接口的参数为两个Runnable接口。这两个两个Runnable接口分别代表了为true或false时要进行的操作
2编写判断方法 创建一个名为isTureOrFalse的方法,方法的返回值为刚才定义的函数式接口-BranchHandle。
3效果

如果存在值执行消费操作,否则执行基于空的操作

1定义函数 创建一个名为PresentOrElseHandler的函数式接口,接口的参数一个为Consumer接口。一个为Runnable,分别代表值不为空时执行消费操作和值为空时执行的其他操作
2编写判断方法 创建一个名为isBlankOrNoBlank的方法,方法的返回值为刚才定义的函数式接口-PresentOrElseHandler。
3使用方式 调用工具类参数参数后,调用函数式接口的presentOrElseHandle方法传入一个Consumer和Runnable

结尾

Function函数式接口是java 8非常重要的特性,利用好Function函数可以极大的简化代码。