A "lambda expression " is an anonymous function that can contain expressions and statements and can be used to create delegates or expression tree types.
All Lambda expressions use the Lambda operator => , which reads "goes to" . The left side of this lambda operator is the input parameter (if any), and the right side contains the expression or statement block. The lambda expression x=> x * x is read as "x goes to x times x" . This expression can be assigned to a delegate type
E.g:
Integer[] a = {1, 8, 3, 9, 2, 0, 5}; Arrays.sort(a, (o1, o2) => o1 - o2);
Lambda expression syntax
The basic structure of Lambda is (arguments)-> body , in the following cases:
- When the parameter type can be deduced, there is no need to specify the type, such as (a) -> System.out.println(a)
- When there is only one parameter and the type can be deduced, write () is not mandatory , such as a -> System.out.println(a)
- When the parameter specifies the type, there must be parentheses, such as (int a) -> System.out.println(a)
- The parameter can be empty, such as () -> System.out.println("hello")
- body needs to contain statements with {} , and {} can be omitted when there is only one statement