GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Java Lambda Expressions - GeeksforGeeks Java Lambda ExpressionsLast Updated : 29 Aug, 2026 lambda expressions, introduced in Java 8, allow developers to write concise, functional-style code by representing anonymous functions. They enable passing code as parameters or assigning it to variables, resulting in cleaner and more readable programs.
Output Addition: 15 Multiplication: 50 Subtraction: 5 Explanation: The Calculator interface is a functional interface because it has one abstract method, calculate(). The add, multiply, and subtract variables store different lambda expressions that provide implementations of this method. When calculate() is called, the corresponding lambda expression is executed. Syntax
![]()
Output Sum: 30 Explanation: In Above example, the Add functional interface defines the addition() method that takes two integers and returns their sum. The lambda expression (a, b) -> a + b provides the implementation of this method without creating a separate class. The addition() method is then called with 10 and 20, producing 30. Functional interfaceA functional interface has exactly one abstract method. Lambda expressions provide its implementation. @FunctionalInterface annotation is optional but recommended to enforce this rule at compile time.
Output 10 Explanation: In above example, FuncInterface contains one abstract method, abstractFun(), and one default method, normalFun(). The lambda expression (int x) -> System.out.println(2 * x) provides the implementation of the abstract method. When abstractFun(5) is called, the value 5 is multiplied by 2, so the output is 10. Types of Lambda ParametersThere are three Lambda Expression Parameters are mentioned below: 1. Lambda with Zero ParametersLambda with Zero Parameters is a lambda expression that does not take any input values. It is commonly used to implement methods that perform a task without requiring any arguments.
Syntax:
Output This is a zero-parameter lambda expression! Explanation: In above example, the ZeroParameter functional interface defines a display() method that takes no arguments. The lambda expression () -> System.out.println(...) implements this method. When display() is called, it prints the given message. Empty parentheses () indicate that the lambda does not accept any parameters. 2. Lambda with a Single ParameterLambda with a Single Parameter is a lambda expression that accepts one input value. It is used when the operation needs a single argument to produce a result or perform a task.
Syntax:
Output All elements: 1 2 3 Even elements: 2 Explanation: In above example, an ArrayList contains the numbers 1, 2, and 3. The lambda expression n -> System.out.println(n) is used with forEach() to print every element. Another lambda checks n % 2 == 0 and prints only the even numbers. Since the lambda has one parameter, parentheses around n are optional.
3. Lambda Expression with Multiple ParametersLambda with Multiple Parameters is a lambda expression that accepts two or more input values. It is used when an operation requires multiple arguments to perform a calculation or execute a task.
Syntax:
Output 9 20 Explanation: In above example, the Functional interface defines an operation() method that accepts two integers. Two lambda expressions provide different implementations of this method: (a, b) -> a + b performs addition, while (a, b) -> a * b performs multiplication. The corresponding method calls produce 9 and 20. Examples in Collections / StreamsLambda expressions are widely used with Java Collections and Streams for concise operations
Output All names: Alice Bob Charlie Adam Names starting with 'A': ALICE ADAM Explanation: In above example, a list containing four names is created. The forEach() method uses a lambda expression to print every name. A Stream is then used with filter() to select names beginning with "A", map() to convert them to uppercase, and forEach() to print the results. This demonstrates how lambda expressions can make collection and stream operations concise. Common Built-in Functional Interfaces
Validity Check: Example Lambda Expressions
Advantages
Comment Explore | |||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |