Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces
lambda expressions are added in Java 8 and provide below functionalities.
- Enable to treat functionality as a method argument, or code as data.
- A function that can be created without belonging to any class.
- A lambda expression can be passed around as if it was an object and executed on demand.
lambda operator -> body
where lambda operator can be:
- Zero parameter:
() -> System.out.println("Zero parameter lambda");
- One parameter:–
(p) -> System.out.println("One parameter: " + p);
It is not mandatory to use parentheses, if the type of that variable can be inferred from the context
- Multiple parameters :
(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);
Please note: Lambda expressions are just like functions and they accept parameters just like functions.
1 2 3 4 2 4
Note that lambda expressions can only be used to implement functional interfaces. In the above example also, the lambda expression implements Consumer Functional Interface.
A Java program to demonstrate working of lambda expression with two arguments.
Addition is 9 Multiplication is 18 Hello Geek
- The body of a lambda expression can contain zero, one or more statements.
- When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.
- When there are more than one statements, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.
Please see this. for an application.
This article is contributed by Sampada Kaushal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Lambda Expressions | Concurrent Programming Approach 4
- Java Lambda Expression with Collections
- Check if a string contains only alphabets in Java using Lambda expression
- Regular Expressions in Java
- How to validate a Password using Regular Expressions in Java
- How to validate a Username using Regular Expressions in Java
- How to validate an IP address using Regular Expressions in Java
- Difference between Anonymous Inner Class and Lambda Expression
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.BiPredicate interface in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples