Lambda in Java
A lambda is a set of instructions that can be separated into a separate variable and then called multiple times in different places in the program.
The basis of a lambda expression is the lambda operator, which represents the arrow ->. This operator divides the lambda expression into two parts: the left part contains the list of parameters of the expression, and the right part itself represents the body of the lambda expression, where all the actions are performed.
The lambda expression does not execute on its own, but forms the implementation of the method defined in the functional interface. It is important that the functional interface should contain only one single method without implementation.
interface Operationable {
int calculate(int x, int y);
}
public static void main(String[] args) {
Operationable operation = (x, y) -> x + y;
int result = operation.calculate(10, 20);
System.out.println(result); //30
}
In fact, lambda expressions are a kind of shorthand for the internal anonymous classes that were previously used in Java.
- Deferred execution of lambda expressions is defined once in one place in the program, called if necessary, any number of times and in an arbitrary place in the program.
- The parameters of the lambda expression must match the parameters of the functional interface method in type:
operation = (int x, int y) -> x + y; // When writing the lambda expression itself, // the type of parameters is allowed not to be specified: (x, y) -> x + y; // If the method takes no parameters, // then empty parentheses are written, for example, () -> 30 + 20; // If the method takes only one parameter, // then the parentheses can be omitted: n -> n * n;
-
Final lambda expressions are not required to return any value.
interface Printable { void print(String s); } public static void main(String[] args) { Printable printer = s -> System.out.println(s); printer.print("Hello, world"); }
-
Block lambda expressions are surrounded by curly braces. In block lambda expressions, you can use inner nested blocks, loops, if statements, switch statements, create variables, etc. If a block lambda expression must return a value, then the return statement is explicitly applied:
Operationable operation = (int x, int y) -> { if (y == 0) { return 0; } else { return x / y; } };
- Passing a lambda expression as a method parameter:
interface Condition { boolean isAppropriate(int n); } private static int sum(int[] numbers, Condition condition) { int result = 0; for (int i : numbers) { if (condition.isAppropriate(i)) { result += i; } } return result; } public static void main(String[] args) { System.out.println(sum(new int[] {0, 1, 0, 3, 0, 5, 0, 7, 0, 9}, (n) -> n != 0)); }
What variables do lambda expressions have access to?
Accessing external scope variables from a lambda expression is very similar to accessing from anonymous objects. You can refer to:
- local variables that are effectively final (not necessarily marked as final);
- class fields;
- static variables.
The default methods of an implemented functional interface are not allowed to be accessed within a lambda expression.
How do I sort a list of strings using a lambda expression?
public static List<String> sort(List<String> list){
Collections.sort(list, (a, b) -> a.compareTo(b));
return list;
}
Read also:
- HashMap details in Java
- TreeSet, HashSet, LinkedHashSet in Java
- List of innovations have appeared in Java 8 and JDK 8
Comments
Post a Comment