Lambda表达式在Java 8中的简单应用
我一直試圖在我編寫的代碼中放入lambda表達式,而這個簡單的例子就是相同的結果。 對于那些完全不了解Java中的Lambda表達式的人,我建議他們在進入本文之前先閱讀此內容 。
好的,現在您已經熟悉了Lambda表達式(在閱讀了介紹性文章之后),讓我們進入一個簡單的示例,我認為它是Lambda表達式的一種很好的用法。
考慮這種情況:某種操作被一些預處理和一些后處理所包圍。 并且要執行的操作可能會根據
預期的行為。 預處理代碼提取操作所需的參數,后處理進行必要的清理。
讓我們看看如何通過匿名內部類使用接口及其實現來完成此工作。
使用匿名內部類
必須實現以提供所需行為的接口:
interface OldPerformer {public void performTask(String id, int status); }讓我們看一下執行預處理,執行所需操作然后進行后處理的方法:
public class PrePostDemo {static void performTask(String id, OldPerformer performer) {System.out.println("Pre-Processing...");System.out.println("Fetching the status for id: " + id);int status = 3;//Some status value fetchedperformer.performTask(id, status);System.out.println("Post-processing...");} }我們需要傳遞2件事-一個標識符來執行預處理和該操作的實現,如下所示:
public class PrePostDemo {public static void main(String[] args) {//has to be declared final to be accessed within//the anonymous inner class.final String outsideOfImpl = "Common Value";performTask("1234", new OldPerformer() {@Overridepublic void performTask(String id, int status) {System.out.println("Finding data based on id...");System.out.println(outsideOfImpl);System.out.println("Asserting that the status matches");}});performTask("4567", new OldPerformer() {@Overridepublic void performTask(String id, int status) {System.out.println("Finding data based on id...");System.out.println(outsideOfImpl);System.out.println("Update status of the data found");}});} }如上所述,在匿名內部類外部聲明的變量必須聲明為最終變量,才能在匿名內部類的方法中訪問它們。 上面代碼的輸出為:
Pre-Processing... Fetching the status for id: 1234 Finding data based on id... Common Value Asserting that the status matches Post-processing... Pre-Processing... Fetching the status for id: 4567 Finding data based on id... Common Value Update the status of the data found Post-processing...使用Lambda表達式
讓我們看看如何使用lambda表達式編寫上面的代碼:
public class PrePostLambdaDemo {public static void main(String[] args) { //Need not be declared as final for use within a //lambda expression, but has to be eventually final.String outsideOfImpl = "Common Value";doSomeProcessing("123", (String id, int status) -> {System.out.println("Finding some data based on"+id);System.out.println(outsideOfImpl);System.out.println("Assert that the status is "+status );});doSomeProcessing("456", (String id, int status) -> {System.out.print("Finding data based on id: "+id);System.out.println(outsideOfImpl);System.out.println("And updating the status: "+status);});}static void doSomeProcessing(String id, Performer performer ){System.out.println("Pre-Processing...");System.out.println("Finding status for given id: "+id);int status = 2;performer.performTask(id, status);System.out.println("Post-processing...");} }interface Performer{ public void performTask(String id, int status); }除了有趣的lambda表達式語法外,lambda表達式范圍之外的變量未聲明為final。 但這必須是最終的,這意味著變量的值:outsideOfImpl一旦聲明就不應修改。
這只是使用lambda表達式代替匿名內部類的另一種更簡潔的方法。
分手說明:JDK 8的計劃發布已進一步推遲到2014年2月,可以在此處找到完整的計劃。 我正在使用lambda項目,該項目每天都在更新,因此,請隨時告訴我這種情況是否不適用于最新版本。 我將盡我所能繼續更新構建,并嘗試這里發布的示例。
另一個注意事項:不要對Java 8中發生的事情不知所措,這些功能現在已經成為許多編程語言的一部分。 我發現學習Java中的lambda表達式的語法和方法有助于我理解和思考功能,更具體地說,是了解Scala閉包。
翻譯自: https://www.javacodegeeks.com/2013/05/a-simple-application-of-lambda-expressions-in-java-8.html
總結
以上是生活随笔為你收集整理的Lambda表达式在Java 8中的简单应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为笔记本电脑对比度(华为笔记本电脑对比
- 下一篇: Spring JMS,消息自动转换,JM