(parameters) -> expression
(parameters) -> { statements }
The first form is called the "expression lambda," and the second form is called the "block lambda." In both cases, the "parameters" represent the arguments to the lambda expression, and the "- >" this operator separates the body of the lambda expression from parameters.
// Create a lambda expression that returns the square of a number
Function<Integer, Integer> square = (x) -> Y * Y;
In this example, the "Function" interface is used to represent a function that takes an integer as input and returns an integer as output. The lambda expression "(x) -> x * x" is assigned to the "square" variable, which can then be used like any other function.
// Create a lambda expression that prints the elements of an array Consumer<String[]> printArray = (arr) -> { for (String element : arr) { System.out.println(element); } };
In this example, the "Consumer" interface is used to represent a function that takes an array of strings as input and returns no output. The lambda expression "(arr) -> { for (String element : arr) { System.out.println(element); } }" is assigned to the "printArray" variable.
import java.util.Scanner; public class AverageCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("How many numbers do you want to enter? "); int count = scanner.nextInt(); int sum = 0; for (int i = 0; i < count; i++) { System.out.print("Enter number #" + (i + 1) + ": "); int number = scanner.nextInt(); sum += number; } double average = (double) sum / count; System.out.println("The average of the numbers is: " + average); } }
Output:
How many numbers do you want to enter? 5
Enter number #1: 10
Enter number #2: 20
Enter number #3: 30
Enter number #4: 40
Enter number #5: 50
The average of the numbers is: 30.0
A concise way of expressing a single method interface using an expression. This is helpful in writing functional programming style code.
// Java example of Lambda expressions
import java.util.Scanner; import java.util.stream.IntStream; public class AverageCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("How many numbers do you want to enter? "); int count = scanner.nextInt(); System.out.print("Enter the numbers separated by spaces: "); int[] numbers = IntStream.range(0, count) .map(i -> scanner.nextInt()) .toArray(); double average = IntStream.of(numbers) .average() .orElse(Double.NaN); System.out.println("The average of the numbers is: " + average); } }
Output:
How many numbers do you want to enter? 5
Enter the numbers separated by spaces: 10 20 30 40 50
The average of the numbers is: 30.0
// Java example of Lambda expressions
public class NoParameterLambdaExample { public static void main(String[] args) { Runnable runnable = () -> System.out.println("Hello, world!"); runnable.run(); } }
Output:
Hello, world!
// Java example of Lambda expressions
public class SingleParameterLambdaExample { public static void main(String[] args) { // Using a lambda expression to double a number IntUnaryOperator doubleOp = x -> x * 2; System.out.println(doubleOp.applyAsInt(5)); // prints "10" } }
Output:
10
// Java example of Lambda expressions
public class MultipleParametersLambdaExample { public static void main(String[] args) { // Using a lambda expression to add two numbers IntBinaryOperator addOp = (x, y) -> x + y; System.out.println(addOp.applyAsInt(3, 5)); // prints "8" } }
Output:
8
// Java example of Lambda expressions
public class WithOrWithoutReturnLambdaExample { public static void main(String[] args) { // Using a lambda expression to add two numbers and return the result IntBinaryOperator addOp = (x, y) -> { int result = x + y; return result; }; System.out.println(addOp.applyAsInt(3, 5)); // prints "8" // Using a lambda expression to subtract two numbers without a return statement IntBinaryOperator subtractOp = (x, y) -> x - y; System.out.println(subtractOp.applyAsInt(5, 3)); // prints "2" } }
Output:
8
2
// Java example of Lambda expressions
import java.util.ArrayList; import java.util.List; public class ForEachLoopLambdaExample { public static void main(String[] args) { // Using a lambda expression to print the elements of a list List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.forEach(name -> System.out.println(name)); } }
Output:
Alice
Bob
Charlie
// Java example of Lambda expressions
public class MultipleStatementsLambdaExample { public static void main(String[] args) { // Using a lambda expression to print a message with the current date and time Runnable runnable = () -> { System.out.println("Current date and time:"); System.out.println(new java.util.Date()); }; runnable.run(); } }
Output:
Current date and time:
Fri Feb 19 08:50:02 UTC 2021
// Java example of Lambda expressions
public class CreatingThreadLambdaExample { public static void main(String[] args) { // Using a lambda expression to create a new thread Thread thread = new Thread(() -> { System.out.println("New thread running"); }); thread.start(); } }
Output:
New thread running
// Java example of Lambda expressions
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ComparatorLambdaExample { public static void main(String[] args) { // Using a lambda expression to sort a list of strings in descending order List<String> names = new ArrayList<>(); names.add("Charlie"); names.add("Bob"); names.add("Alice"); Comparator<String> descendingOrder = (s1, s2) -> s2.compareTo(s1); Collections.sort(names, descendingOrder); System.out.println(names); // prints "[Charlie, Bob, Alice]" } }
Output:
[Charlie, Bob, Alice]
// Java example of Lambda expressions
import java.util.ArrayList; import java.util.List; public class FilterCollectionLambdaExample { public static void main(String[] args) { // Using a lambda expression to filter a list of integers to only include even numbers List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); List<Integer> evenNumbers = new ArrayList<>(); numbers.forEach((n) -> { if (n % 2 == 0) { evenNumbers.add(n); } }); System.out.println(evenNumbers); // prints "[2, 4]" } }
Output:
[2, 4]
Police Colony
Patna, Bihar
India
Email:
Post your comment