Java Lambda Expressions are a new feature introduced in Java 8 that allows us to write more concise and functional code. Lambda expressions provide a way to create anonymous functions that can be passed as parameters to other functions or stored in variables.
The basic syntax for a lambda expression is as follows:
(parameters) -> expression
Here, the parameters are the input parameters for the function, and the expression is the body of the function.
Let’s consider an example to better understand lambda expressions. Suppose we have a list of integers, and we want to find the sum of all the even numbers in the list. Here is how we could do it using a lambda expression:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); int sum = numbers.stream() .filter(n -> n % 2 == 0) .mapToInt(n -> n) .sum(); System.out.println(sum);
In this example, we first create a list of integers using the Arrays.asList() method. We then use the stream() method to create a stream from the list, which allows us to apply operations to the elements in the list.
We use the filter() method to filter out all the odd numbers in the list. The filter() method takes a lambda expression as a parameter, which is used to test each element in the stream. Here, the lambda expression
n -> n % 2 == 0 tests whether the given integer is even or not.
We then use the mapToInt() method to convert the stream of integers to a stream of ints, which is required to use the sum() method. Finally, we call the sum() method to get the sum of all the even numbers in the list.
Lambda Expression with Multiple Parameters
Lambda expressions can also take multiple parameters. The syntax for a lambda expression with multiple parameters is as follows:
(parameter1, parameter2, ...) -> expression
Suppose we have a list of strings, and we want to sort the list based on the length of the strings. Here is how we could do it using a lambda expression with multiple parameters:
List<String> strings = Arrays.asList("apple", "banana", "cherry", "date"); Collections.sort(strings, (s1, s2) -> s1.length() - s2.length()); System.out.println(strings);
In this example, we first create a list of strings using the Arrays.asList() method. We then use the sort() method of the Collections class to sort the list based on the length of the strings.
The sort() method takes two parameters: the list to be sorted and a comparator function. Here, we use a lambda expression as the comparator function to compare the length of two strings. The lambda expression
(s1, s2) -> s1.length() - s2.length() takes two parameters (s1 and s2), and returns the difference between the length of s1 and s2. This lambda expression is used to compare two strings, and the sort() method uses this comparison function to sort the list.
Lambda Expression with Block of Code:
Sometimes, we might need to write more complex code in a lambda expression, such as a block of code with multiple statements. In such cases, we can enclose the code in curly braces
Suppose we have a list of integers, and we want to print out all the even numbers in the list. Here is how we could do it using a lambda expression with a block of code:
List<Integer> numbers = Arrays.asList(1