- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 181字
- 2021-07-02 19:01:56
Lambda expressions
Lambda expressions are the brand new feature of Java. Lambda expressions are introduced in Java 8 and it is a step towards facilitating functional programming in Java.
Lambda expressions help you to define a method without declaring it. So, you do not need a name for the method, return-type, and so on. Lambda expressions, like anonymous inner classes, provide the way to pass behaviors to functions. Lambda, however, is a much more concise way of writing the code.
For example, the preceding example of an anonymous inner class can be converted to Lambda as follows:
public class MyFilterImpl { public static void main(String[] args) { File dir = new File("src/main/java"); dir.list((dirname,name)->name.endsWith("java")); //Lambda Expression } }
Note that the signature of the Lambda expression is exactly matching the signature of the accept method in the FilenameFilter interface.
One of the huge differences between Lambda and anonymous inner classes lies in the way Lambdas are invoked. Lambdas are invoked dynamically using Method Handle ( MH) calls, unlike anonymous inner classes where a separate inner class is created at compile time for reference.
推薦閱讀
- C++面向對象程序設計(第三版)
- 大學計算機基礎實驗教程
- Vue.js 2 and Bootstrap 4 Web Development
- NGINX Cookbook
- 好好學Java:從零基礎到項目實戰
- HTML+CSS+JavaScript編程入門指南(全2冊)
- 軟件測試綜合技術
- Unity Android Game Development by Example Beginner's Guide
- PowerDesigner 16 從入門到精通
- INSTANT Apache Hive Essentials How-to
- HTML5游戲開發實戰
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- Java RESTful Web Service實戰
- Java Web動態網站開發(第2版·微課版)
- Unreal Engine Game Development Cookbook