- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 182字
- 2021-07-02 19:01:56
Syntax of Lambda expressions
A Lambda expression consists of two parts: 0 or more arguments and the function body:
argument -> function (argument1,argument2,...argument) -> function
Let's discuss it with examples. Consider a list of integers as follows:
List<Integer> list = Arrays.asList(1,2,3,4,5);
Print each element of the list using Lambda:
list.forEach(n-> System.out.println(n));
Multiply each element of the list by 2:
list.stream().map(n -> n*2 );
The stream method will convert the list to a stream of numbers. We will discuss more about it in another section. For now, just consider that the stream will convert all the elements of the list into the stream.
Here, we do not need to specify the type of n. The type of n is auto-inferred in Lambda. Also, we have not specified any return statement. The return statement is implicit in Lambda. If you want, you can specify the return statement explicitly as follows:
list.stream().map(n -> { return n*2; }); }
For specifying the return type explicitly, you need to enclose the function body in braces and provide a semicolon ( ;) at the end of the return statement.
推薦閱讀
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- Java 9 Programming Blueprints
- Python Network Programming Cookbook(Second Edition)
- C語言實驗指導及習題解析
- INSTANT Passbook App Development for iOS How-to
- Linux:Embedded Development
- 人工智能算法(卷1):基礎算法
- 從零學Java設計模式
- Python物理建模初學者指南(第2版)
- Java設計模式深入研究
- Python計算機視覺與深度學習實戰
- JavaScript設計模式與開發實踐
- Roslyn Cookbook
- Mastering VMware vSphere Storage
- Qt編程快速入門