SlideShare a Scribd company logo
Java Lambda Expressions
Murali D
Lambda Expressions
Lambda expressions were introduced in Java 8 and are a powerful feature that
allows you to pass behavior as parameters to methods. They provide a clear and
concise way to express instances of single-method interfaces (functional
interfaces) using an expression.
Lambda expressions are commonly used to define the implementation of
functional interfaces in a more compact way. They are particularly useful for
working with the Java Streams API, but they can be used in any context where a
functional interface is required.
Syntax of Lambda Expressions
(parameters) -> expression
(Or, if there's a block of code)
(parameters) -> { statements; }
Components of a Lambda Expression:
● Parameters: The input parameters (if any) to the lambda expression,
enclosed in parentheses. The types can often be inferred, so you may omit
them (type inference).
● Arrow Token (->): Separates the parameters from the body.
● Expression or Block of Code: The body of the lambda expression. If it's a
single expression, it can return a value directly. If it's a block of code, it may
contain multiple statements.
Simple Lambda Expression
// A lambda expression that adds two numbers
BinaryOperation add = (a, b) -> a + b;
System.out.println(add.operation(5, 3)); // Output: 8
Lambda Expression with Multiple Parameters:
// Lambda expression with two parameters
BiPredicate<Integer, Integer> isGreater = (x, y) -> x > y;
System.out.println(isGreater.test(10, 5)); // Output: true
Lambda Expression with No Parameters:
Runnable helloWorld = () -> System.out.println("Hello, World!");
helloWorld.run(); // Output: Hello, World!
Lambda Expression with Multiple Statements:
Function<Integer, Integer> squareAndAddOne = (x) -> {
int square = x * x;
return square + 1;
};
System.out.println(squareAndAddOne.apply(5)); // Output: 26
Benefits of Lambda Expressions
Concise Code: They allow you to express instances of single-method interfaces in
a more compact and readable form.
Cleaner Code: They make your code more functional and less cluttered by
reducing boilerplate code, especially in scenarios like event handling, threading,
etc.
Improved Readability: Code that was previously verbose can now be written more
clearly, especially when using the Streams API or when working with collections.
Increased Flexibility: You can pass behavior (code) as parameters, which allows
you to write more generic and reusable code.
Streams API
The Streams API provides a high-level abstraction for performing complex data manipulations (such as
filtering, mapping, and reducing) on collections in a functional style.
It allows you to perform operations on collections in a more declarative, functional, and parallelizable
manner.
Common Stream Operations:
● filter: Filters elements based on a condition.
● map: Transforms each element.
● reduce: Reduces elements to a single value.
● forEach: Iterates over elements.
● collect: Collects elements into collections (e.g., lists, sets).
Lambda Expressions with Streams
Lambda expressions are commonly used with the Streams API, which is part of Java's functional programming
capabilities.
The Streams API allows you to process sequences of elements in a functional style.
Example: Using lambda expression to filter and print even numbers
import java.util.List;
import java.util.Arrays;
public class LambdaWithStream {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(n -> System.out.println(n)); // Output: 2 4
}
}

More Related Content

PPTX
Java 8 - An Overview
PPTX
Java8 training - Class 1
PPTX
Insight into java 1.8, OOP VS FP
PPTX
C#-LINQ-and-Lambda-Expression
PPT
14274730 (1).ppt
PPTX
Lambdas, Collections Framework, Stream API
PDF
Lambda Expressions in Java
PPTX
Functional programming
Java 8 - An Overview
Java8 training - Class 1
Insight into java 1.8, OOP VS FP
C#-LINQ-and-Lambda-Expression
14274730 (1).ppt
Lambdas, Collections Framework, Stream API
Lambda Expressions in Java
Functional programming

Similar to Java Advanced Topic - Java Lambda Expressions.pptx (20)

DOCX
Colloquium Report
PDF
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
PDF
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
PDF
Unit-3.pptx.pdf java api knowledge apiii
PDF
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
PPTX
Lambda Expressions Java 8 Features usage
PPTX
Java 8 lambda
PPT
C#3.0 & Vb 9.0 New Features
PPTX
Gude for C++11 in Apache Traffic Server
PPTX
Functions in C++
PPT
Lambdas
PPTX
Functional Programming In Jdk8
PDF
Smart Migration to JDK 8
PPT
Scala Talk at FOSDEM 2009
PDF
New c sharp3_features_(linq)_part_ii
PDF
Java 8
PDF
Functional programming in java 8 by harmeet singh
PDF
Java 8 features
PPTX
Intermediate Representation in Compiler Construction
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
Colloquium Report
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
Unit-3.pptx.pdf java api knowledge apiii
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Lambda Expressions Java 8 Features usage
Java 8 lambda
C#3.0 & Vb 9.0 New Features
Gude for C++11 in Apache Traffic Server
Functions in C++
Lambdas
Functional Programming In Jdk8
Smart Migration to JDK 8
Scala Talk at FOSDEM 2009
New c sharp3_features_(linq)_part_ii
Java 8
Functional programming in java 8 by harmeet singh
Java 8 features
Intermediate Representation in Compiler Construction
A Brief Conceptual Introduction to Functional Java 8 and its API
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Lesson notes of climatology university.
PPTX
Cell Types and Its function , kingdom of life
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPH.pptx obstetrics and gynecology in nursing
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
Lesson notes of climatology university.
Cell Types and Its function , kingdom of life
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
STATICS OF THE RIGID BODIES Hibbelers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Ad

Java Advanced Topic - Java Lambda Expressions.pptx

  • 2. Lambda Expressions Lambda expressions were introduced in Java 8 and are a powerful feature that allows you to pass behavior as parameters to methods. They provide a clear and concise way to express instances of single-method interfaces (functional interfaces) using an expression. Lambda expressions are commonly used to define the implementation of functional interfaces in a more compact way. They are particularly useful for working with the Java Streams API, but they can be used in any context where a functional interface is required.
  • 3. Syntax of Lambda Expressions (parameters) -> expression (Or, if there's a block of code) (parameters) -> { statements; } Components of a Lambda Expression: ● Parameters: The input parameters (if any) to the lambda expression, enclosed in parentheses. The types can often be inferred, so you may omit them (type inference). ● Arrow Token (->): Separates the parameters from the body. ● Expression or Block of Code: The body of the lambda expression. If it's a single expression, it can return a value directly. If it's a block of code, it may contain multiple statements.
  • 4. Simple Lambda Expression // A lambda expression that adds two numbers BinaryOperation add = (a, b) -> a + b; System.out.println(add.operation(5, 3)); // Output: 8
  • 5. Lambda Expression with Multiple Parameters: // Lambda expression with two parameters BiPredicate<Integer, Integer> isGreater = (x, y) -> x > y; System.out.println(isGreater.test(10, 5)); // Output: true
  • 6. Lambda Expression with No Parameters: Runnable helloWorld = () -> System.out.println("Hello, World!"); helloWorld.run(); // Output: Hello, World!
  • 7. Lambda Expression with Multiple Statements: Function<Integer, Integer> squareAndAddOne = (x) -> { int square = x * x; return square + 1; }; System.out.println(squareAndAddOne.apply(5)); // Output: 26
  • 8. Benefits of Lambda Expressions Concise Code: They allow you to express instances of single-method interfaces in a more compact and readable form. Cleaner Code: They make your code more functional and less cluttered by reducing boilerplate code, especially in scenarios like event handling, threading, etc. Improved Readability: Code that was previously verbose can now be written more clearly, especially when using the Streams API or when working with collections. Increased Flexibility: You can pass behavior (code) as parameters, which allows you to write more generic and reusable code.
  • 9. Streams API The Streams API provides a high-level abstraction for performing complex data manipulations (such as filtering, mapping, and reducing) on collections in a functional style. It allows you to perform operations on collections in a more declarative, functional, and parallelizable manner. Common Stream Operations: ● filter: Filters elements based on a condition. ● map: Transforms each element. ● reduce: Reduces elements to a single value. ● forEach: Iterates over elements. ● collect: Collects elements into collections (e.g., lists, sets).
  • 10. Lambda Expressions with Streams Lambda expressions are commonly used with the Streams API, which is part of Java's functional programming capabilities. The Streams API allows you to process sequences of elements in a functional style. Example: Using lambda expression to filter and print even numbers import java.util.List; import java.util.Arrays; public class LambdaWithStream { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); numbers.stream() .filter(n -> n % 2 == 0) .forEach(n -> System.out.println(n)); // Output: 2 4 } }