SlideShare a Scribd company logo
6
Most read
9
Most read
17
Most read
Lambda Expressions – Java 8
Content
• Understanding Lambdas
• Using Lambdas
• Functional Interface
• Method references
• Collections improvements
Why Lambdas?
• Enables Functional Programming
• Readable and concise code
• Enables us to write better APIs and libraries which are easier to use
• Support for parallel processing
Functional Programming VS OOP
• Functional programming allow you to write more readable and
maintainable code.(in some cases)
• Problems in OOP
• Everything is object - all code blocks are "associated" with class and objects ,
which is a problem some time
• We cannot have a independent functions , it have to be a part of function.
• Also allow us to use functions as values.
Function as a value
aBlockOfCode = public void () {
System.out.println("Hello World");
}
perfom
->
aBlockOfCode = () -> {
System.out.println("Hello World");
}
aBlockOfCode = () -> System.out.println("Hello World");
Type Inference
• Your lambda Expression infer type of function and its return type
from the interface(functional interface).
public static void main(String[] args)
{
//StringLengthLambda myStringLenthLambda = (String s) -> s.length();
//StringLengthLambda myStringLenthLambda = (s) -> s.length();
StringLengthLambda myStringLenthLambda = s -> s.length();
System.out.println("String Length :: " + myStringLenthLambda.getLength("Hello World!!"));
}
interface StringLengthLambda
{
int getLength(String s);
}
Lambda Vs Interface Implementation
• We can implement the functional interface in two ways.
• By Implementing the lambda Expression
• By Creating the Anonymous class implementation
Greeting innerClassGreeting = new Greeting() {
@Override
public void perfom() {
System.out.println("Hello World");
}
};
innerClassGreeting.perfom();
Greeting myLambdaFunction = () -> System.out.println("Hello World");
myLambdaFunction.perfom();
Lambda Vs Interface Implementation
• For the most purposes you can think of the lambda expression as the
shortcut for creating these kind of anonymous inner classes but its
not exactly true.
• There are things that inner class does which is different from what
these lambda expressions do.
• In case of lambda expression its not exactly creating an anonymous
class, will look at those differences in a further slides.
Runnable Using Lambdas
• You can use lambda in order to create Runnable.
• Implement Using anonymous inner class
• Implement Using Lambda Expression
public static void main(String[] args) {
Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Printed inside runnable");
}
});
myThread.start();
}
Thread myLambdaThread = new Thread(() -> System.out.println("Printed inside Lambda Runnable"));
myLambdaThread.start();
Functional Interface
• Any interface with a SAM(Single Abstract Method) is a functional
interface
• Interface can have multiple default methods but it should have only
one abstract method(Java 8)
• its implementation may be treated as lambda expressions.
• It's recommended that all functional interfaces have an informative
@FunctionalInterface annotation.
• Add @FunctionalInterface on Greeting
Implementation Of Lambda Expression
• Lets do some hands on exercise to understand advantages of lambda
expression with collections.
Predicate Interface
• Represents a predicate (boolean-valued function) of one argument.
• This is a functional interface whose functional method is test(Object).
Consumer Interface
• Represents an operation that accepts a single input argument and
returns no result.
• This is a functional interface whose functional method is
accept(Object).
Exception Handling in lambda Expression
• Create a lambda expression which X in an array of integers and a key
which is and do the mathematical operation for each element in array
given a key.
Closers in Lambda Expressions
• Understand what is effectively final.
• Understand the freeze value in lambda expression
Difference between lambda and Anonymous
Inner Class.
• In an anonymous inner class when using the this reference , it
overwrite the this reference to the anonymous inner class instance.
• In the case of a lambda that does not happen. It still refers to the
instance that it points to the outside of the lambda(no overriding of
this reference that happens).
• Lets have an example in eclipse.
Streams
• A sequence of elements supporting sequential and parallel aggregate
operations.
• Stream Operations
• Intermediate Operations (Return Stream Object)
• Map
• Filter
• Sorted
• Terminal Operations
• Collect
• forEach
• Reduce
• Stream Operations don’t change the source.
Optional
• The purpose of the class is to provide a type-level solution for
representing optional values instead of null references
• Static methods of Optional class
• empty()
• of()
• isPresent()
Date Time API
• Java 8 introduces a new date-time API under the package
java.time. Following are some of the important classes
introduced in java.time package.
• Instant
• LocalDate
• LocalTime
• LocateDateTime

More Related Content

PDF
Lambdas in Java 8
PDF
Java 8 Lambda Expressions & Streams
PPTX
Functional Programming In Jdk8
PPT
Lambdas
KEY
Java Closures
PPTX
Scala, Play 2.0 & Cloud Foundry
PDF
Functional programming in java 8 by harmeet singh
PPTX
Insight into java 1.8, OOP VS FP
Lambdas in Java 8
Java 8 Lambda Expressions & Streams
Functional Programming In Jdk8
Lambdas
Java Closures
Scala, Play 2.0 & Cloud Foundry
Functional programming in java 8 by harmeet singh
Insight into java 1.8, OOP VS FP

Similar to Lambda Expressions Java 8 Features usage (20)

PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PDF
Unit-3.pptx.pdf java api knowledge apiii
PDF
20160520 what youneedtoknowaboutlambdas
PPT
14274730 (1).ppt
PPTX
Eclipse Day India 2015 - Java 8 Overview
PPTX
10 Sets of Best Practices for Java 8
PDF
JSR 335 / java 8 - update reference
PDF
Android course session 5 (Threads & socket)
PPTX
Scala-Ls1
PPTX
A brief tour of modern Java
PDF
Charles Sharp: Java 8 Streams
PPTX
Beginning Java for .NET developers
PPTX
JAVA 8 Parallel Stream
PPTX
What`s New in Java 8
PDF
Java8 features
PDF
The Scheme Language -- Using it on the iPhone
PPTX
Functional Interfaces and Method References.pptx
PPTX
Scala final ppt vinay
PDF
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Unit-3.pptx.pdf java api knowledge apiii
20160520 what youneedtoknowaboutlambdas
14274730 (1).ppt
Eclipse Day India 2015 - Java 8 Overview
10 Sets of Best Practices for Java 8
JSR 335 / java 8 - update reference
Android course session 5 (Threads & socket)
Scala-Ls1
A brief tour of modern Java
Charles Sharp: Java 8 Streams
Beginning Java for .NET developers
JAVA 8 Parallel Stream
What`s New in Java 8
Java8 features
The Scheme Language -- Using it on the iPhone
Functional Interfaces and Method References.pptx
Scala final ppt vinay
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Ad

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
GDM (1) (1).pptx small presentation for students
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Module 4: Burden of Disease Tutorial Slides S2 2025
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
Ad

Lambda Expressions Java 8 Features usage

  • 2. Content • Understanding Lambdas • Using Lambdas • Functional Interface • Method references • Collections improvements
  • 3. Why Lambdas? • Enables Functional Programming • Readable and concise code • Enables us to write better APIs and libraries which are easier to use • Support for parallel processing
  • 4. Functional Programming VS OOP • Functional programming allow you to write more readable and maintainable code.(in some cases) • Problems in OOP • Everything is object - all code blocks are "associated" with class and objects , which is a problem some time • We cannot have a independent functions , it have to be a part of function. • Also allow us to use functions as values.
  • 5. Function as a value aBlockOfCode = public void () { System.out.println("Hello World"); } perfom -> aBlockOfCode = () -> { System.out.println("Hello World"); } aBlockOfCode = () -> System.out.println("Hello World");
  • 6. Type Inference • Your lambda Expression infer type of function and its return type from the interface(functional interface). public static void main(String[] args) { //StringLengthLambda myStringLenthLambda = (String s) -> s.length(); //StringLengthLambda myStringLenthLambda = (s) -> s.length(); StringLengthLambda myStringLenthLambda = s -> s.length(); System.out.println("String Length :: " + myStringLenthLambda.getLength("Hello World!!")); } interface StringLengthLambda { int getLength(String s); }
  • 7. Lambda Vs Interface Implementation • We can implement the functional interface in two ways. • By Implementing the lambda Expression • By Creating the Anonymous class implementation Greeting innerClassGreeting = new Greeting() { @Override public void perfom() { System.out.println("Hello World"); } }; innerClassGreeting.perfom(); Greeting myLambdaFunction = () -> System.out.println("Hello World"); myLambdaFunction.perfom();
  • 8. Lambda Vs Interface Implementation • For the most purposes you can think of the lambda expression as the shortcut for creating these kind of anonymous inner classes but its not exactly true. • There are things that inner class does which is different from what these lambda expressions do. • In case of lambda expression its not exactly creating an anonymous class, will look at those differences in a further slides.
  • 9. Runnable Using Lambdas • You can use lambda in order to create Runnable. • Implement Using anonymous inner class • Implement Using Lambda Expression public static void main(String[] args) { Thread myThread = new Thread(new Runnable() { @Override public void run() { System.out.println("Printed inside runnable"); } }); myThread.start(); } Thread myLambdaThread = new Thread(() -> System.out.println("Printed inside Lambda Runnable")); myLambdaThread.start();
  • 10. Functional Interface • Any interface with a SAM(Single Abstract Method) is a functional interface • Interface can have multiple default methods but it should have only one abstract method(Java 8) • its implementation may be treated as lambda expressions. • It's recommended that all functional interfaces have an informative @FunctionalInterface annotation. • Add @FunctionalInterface on Greeting
  • 11. Implementation Of Lambda Expression • Lets do some hands on exercise to understand advantages of lambda expression with collections.
  • 12. Predicate Interface • Represents a predicate (boolean-valued function) of one argument. • This is a functional interface whose functional method is test(Object).
  • 13. Consumer Interface • Represents an operation that accepts a single input argument and returns no result. • This is a functional interface whose functional method is accept(Object).
  • 14. Exception Handling in lambda Expression • Create a lambda expression which X in an array of integers and a key which is and do the mathematical operation for each element in array given a key.
  • 15. Closers in Lambda Expressions • Understand what is effectively final. • Understand the freeze value in lambda expression
  • 16. Difference between lambda and Anonymous Inner Class. • In an anonymous inner class when using the this reference , it overwrite the this reference to the anonymous inner class instance. • In the case of a lambda that does not happen. It still refers to the instance that it points to the outside of the lambda(no overriding of this reference that happens). • Lets have an example in eclipse.
  • 17. Streams • A sequence of elements supporting sequential and parallel aggregate operations. • Stream Operations • Intermediate Operations (Return Stream Object) • Map • Filter • Sorted • Terminal Operations • Collect • forEach • Reduce • Stream Operations don’t change the source.
  • 18. Optional • The purpose of the class is to provide a type-level solution for representing optional values instead of null references • Static methods of Optional class • empty() • of() • isPresent()
  • 19. Date Time API • Java 8 introduces a new date-time API under the package java.time. Following are some of the important classes introduced in java.time package. • Instant • LocalDate • LocalTime • LocateDateTime