我尝试过把Lambda表达式融入到我的代码中,下面的代码例子是此次尝试的结果。对于那些完全不知道Lambda表达式的Java程序员,我强烈建议在继续阅读之前,浏览一下这篇文章。
Ok,现在你已经熟悉Lambda表达式了(在阅读过推荐的Lambda入门文章之后),那我们现在开始学习一个我认为很好的Lambda表达式的例子。
考虑一下这种场景:某些操作在执行之前需要做预处理,执行之后需要做后期处理。待执行的操作会随着行为的不同而变化。预处理会提取出这个操作所需的必要参数,后期处理做一些清理的工作。
我们来看看如何利用接口与接口的匿名实现类模拟这个场景。
使用匿名内部类
一个提供了必要行为方法的接口实现:
1 |
interface OldPerformer { |
2 |
3 |
public void performTask(String id, int status); |
4 |
5 |
} |
接下来是一些预处理和后期处理方法:
01 |
public class PrePostDemo { |
02 |
03 |
static void performTask(String id, OldPerformer performer) { |
04 |
05 |
System.out.println( "Pre-Processing..." ); |
06 |
07 |
System.out.println( "Fetching the status for id: " + id); |
08 |
09 |
int status = 3 ; |
10 |
11 |
//Some status value fetched |
12 |
13 |
performer.performTask(id, status); |
14 |
15 |
System.out.println( "Post-processing..." ); |
16 |
17 |
} |
18 |
19 |
} |
我们需要传递2样东西:预处理所需的标识,以及操作的实现。如下所示:
01 |
public class PrePostDemo { |
02 |
03 |
public static void main(String[] args) { |
04 |
05 |
//has to be declared final to be accessed within |
06 |
07 |
//the anonymous inner class. |
08 |
09 |
final String outsideOfImpl = "Common Value" ; |
10 |
11 |
performTask( "1234" , new OldPerformer() { |
12 |
13 |
@Override |
14 |
15 |
public void performTask(String id, int status) { |
16 |
17 |
System.out.println( "Finding data based on id..." |
18 |
19 |
); |
20 |
21 |
System.out.println(outsideOfImpl); |
22 |
23 |
System.out.println( "Asserting that the status matches" ); |
24 |
25 |
} |
26 |
27 |
}); |
28 |
29 |
performTask( "4567" , new OldPerformer() { |
30 |
31 |
@Override |
32 |
33 |
public void performTask(String id, int status) { |
34 |
35 |
System.out.println( "Finding data based on id..." ); |
36 |
37 |
System.out.println(outsideOfImpl); |
38 |
39 |
System.out.println( "Update status of the data found" ); |
40 |
41 |
} |
42 |
43 |
}); |
44 |
45 |
} |
46 |
47 |
} |
从上面的代码可以看出,匿名内部类外部的变量要想被匿名内部类访问,需要声明成final。例子的代码输出如下:
01 |
Pre-Processing...</pre> |
02 |
Fetching the status for id: 1234 |
03 |
04 |
Finding data based on id... |
05 |
06 |
Common Value |
07 |
08 |
Asserting that the status matches |
09 |
10 |
Post-processing... |
11 |
12 |
PreProcessing... |
13 |
14 |
Fetching the status for id: 4567 |
15 |
16 |
Finding data based on id... |
17 |
18 |
Common Value |
19 |
20 |
Update the status of the data found |
21 |
22 |
Post-processing... |
使用Lambda表达式
我们来看看如何用Lambda表达式实现上述例子:
01 |
public class PrePostLambdaDemo { |
02 |
03 |
public static void main(String[] args) { |
04 |
05 |
//Need not be declared as final for use within a |
06 |
07 |
//lambda expression, but has to be eventually final. |
08 |
09 |
String outsideOfImpl = "Common Value" ; |
10 |
11 |
doSomeProcessing( "123" , (String id, int status) -> { |
12 |
13 |
System.out.println( "Finding some data based on" +id); |
14 |
15 |
System.out.println(outsideOfImpl); |
16 |
17 |
System.out.println( "Assert that the status is " +status ); |
18 |
19 |
}); |
20 |
21 |
doSomeProcessing( "456" , (String id, int status) -> { |
22 |
23 |
System.out.print( "Finding data based on id: " +id); |
24 |
25 |
System.out.println(outsideOfImpl); |
26 |
27 |
System.out.println( "And updating the status: " +status); |
28 |
29 |
}); |
30 |
31 |
} |
32 |
33 |
static void doSomeProcessing(String id, Performer performer ){ |
34 |
35 |
System.out.println( "Pre-Processing..." ); |
36 |
37 |
System.out.println( "Finding status for given id: " +id); |
38 |
39 |
int status = 2 ; |
40 |
41 |
performer.performTask(id, status); |
42 |
43 |
System.out.println( "Post-processing..." ); |
44 |
45 |
} |
46 |
47 |
} |
48 |
49 |
interface Performer{ |
50 |
51 |
public void performTask(String id, int status); |
52 |
53 |
} |
除了有趣的Lambda表达式语法之外,还有一点不同,那就是Lambda表达式外部的变量没有声明成final。但最终变量还是会成为常量,这意味着outsideOfImpl 在声明及初始化之后的值不能被更改。
这个例子只是展示了如何使用清晰明了的Lambda表达式代替匿名内部类。
一个小提示:JDK8的发布时间推迟到了2014年2月,完整的发布时间表可以从这里查阅。我每天都会更新Lambda的构建项目,如果在最新的构建中出现了任何问题,请随时联系我。我会尽最大努力持续构建,并且会在这里发表最新的例子。
另一个提示:不要让Java 8的更新使你不知所措,大部分新特性已经存在于其他编程语言中。我发现学习Lambda表达式的语法和方法可以帮助我以函数式的思维进行思考,对此我还应特别感谢Scala闭包。