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