我一直试图在我编写的代码中放入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