- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are lambda expressions and how to use them in Java?
Lambda expressions are introduced in Java 8. These are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only.
Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.
Syntax
parameter -> expression body
Example
public class Java8Tester { public static void main(String args[]) { Java8Tester tester = new Java8Tester(); //with type declaration MathOperation addition = (int a, int b) -> a + b; //without type declaration MathOperation subtraction = (a, b) -> a - b; //with return statement along with curly braces MathOperation multiplication = (int a, int b) -> { return a * b; }; //without return statement and without curly braces MathOperation division = (int a, int b) -> a / b; System.out.println("10 + 5 = " + tester.operate(10, 5, addition)); System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction)); System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication)); System.out.println("10 / 5 = " + tester.operate(10, 5, division)); //without parenthesis GreetingService greetService1 = message -> System.out.println("Hello " + message); //with parenthesis GreetingService greetService2 = (message) -> System.out.println("Hello " + message); greetService1.sayMessage("Mahesh"); greetService2.sayMessage("Suresh"); } interface MathOperation { int operation(int a, int b); } interface GreetingService { void sayMessage(String message); } private int operate(int a, int b, MathOperation mathOperation) { return mathOperation.operation(a, b); } }
Output
10 + 5 = 15 10 - 5 = 5 10 x 5 = 50 10 / 5 = 2 Hello Mahesh Hello Suresh
- Related Articles
- What are lambda expressions in Java?
- What are block lambda expressions in Java?
- Why we use lambda expressions in Java?
- Are lambda expressions objects in Java?
- What are the characteristics of lambda expressions in Java?
- How to debug lambda expressions in Java?
- What are lambda expressions in C#?
- How can we use lambda expressions with functional interfaces in Java?
- What are the advantages of Lambda Expressions in Java?\n
- What are the scoping rules for lambda expressions in Java?\n
- What are the rules to follow while using exceptions in java lambda expressions?
- Differences between Lambda Expressions and Closures in Java?
- How to implement the listeners using lambda expressions in Java?
- What is the syntax for lambda expressions in Java?
- What HTML forms are and how to use them?

Advertisements