閱讀726 返回首頁    go 阿裏雲 go 技術社區[雲棲]


一個Java 8中簡單Lambda表達式程序

我嚐試過把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閉包。

最後更新:2017-05-23 10:02:15

  上一篇:go  Groovy模板引擎上(基礎模板介紹)
  下一篇:go  Sharing Data Among Threads Without Contention